<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/5/16
 * Time: 下午2:52
 *   1.结算工时分类统计 -- ok
 *   2.进销存报表商品辅料库
 *   3.进销存报表商品配件库
 *   4.进销存报表已结辅料库
 *   5.进销存报表已结配件库
 *   6.毛利率分析 -- ok
 *   7.盘点查询 -- ok
 *   8.入库查询 -- ok
 *   9.维修商品出库明细 -- ok
 *   10.销售领用出库明细 -- non
 */
class Report extends Eloquent
{

    /**
     * @Author:      lyt
     * @DateTime:    2016-11-10 15:15:09
     * @Description: 保险的报表
     */
    public static function get_insurance_detail($begin_date,$end_date,$shop_id,$begin_bill='',$end_bill=''){
        $insurance_detail = array();
        $in=DB::table("insurance")
            ->leftJoin('system_users', 'insurance.referrer', '=', 'system_users.id')
            ->leftJoin('maintenance_engineers', 'insurance.referrer', '=', 'maintenance_engineers.id')
            ->leftJoin('sc_user', 'insurance.referrer', '=', 'sc_user.id')
            ->leftJoin('insurance_species', 'insurance.species', '=', 'insurance_species.id')
            ->leftJoin('insurance_use_nature', 'insurance.use_nature', '=', 'insurance_use_nature.id')
            ->leftJoin('maintenance_shops', 'insurance.website', '=', 'maintenance_shops.id')
            ->leftJoin('insurance_company', 'insurance.accept_insurance_id', '=', 'insurance_company.id');
        if($shop_id){
            $in=$in->where("insurance.website",$shop_id);
        }
        $in=$in->where("insurance.status",1)->whereBetween("insurance.created_at",array($begin_date,$end_date));
        if($begin_bill !== '' && $end_bill !== '') {
            $in = $in->whereBetween("insurance.bill_time", array($begin_bill, $end_bill));
        }
        $in=$in->select("insurance.*","system_users.name as sys_name","sc_user.real_name as sc_name","maintenance_engineers.name as me_name","insurance_species.name as is_name","insurance_use_nature.name as use_nature_name","maintenance_shops.name as shop_name","insurance_company.name as company_name_i")->get();
        foreach ($in as $_os) {
            $referrer_name="";

            switch ($_os->referrer_type) {
                case 'custom':
                    // $referrer_name= $_os->car_no;
                    break;
                case 'service':
                    $referrer_name= $_os->sys_name;
                    break;
                case 'engineer':
                    $referrer_name= $_os->me_name;
                    break;
                case 'sc':
                    $referrer_name= $_os->sc_name;
                    break;
            }
            $jqx=InsuranceDetail::where("insurance_id",$_os->id)->where("type",1)->first();
            $syx=InsuranceDetail::where("insurance_id",$_os->id)->where("type",2)->first();
            $_insurance_detaill = array(
                'id' => $_os->id,//车型
                'car_no' => $_os->car_no,//车型
                'website' => $_os->website,//网店
                'website_name' => $_os->shop_name,//网店名字
                'engine_number' => $_os->engine_number,//发动机号
                'model' => $_os->model,//厂牌型号
                'vin' => $_os->vin,//车架号vin
                'use_nature' => $_os->use_nature,//车辆使用性质
                'use_nature_name' => $_os->use_nature_name,//车辆使用性质
                'purchase_amount' => $_os->purchase_amount,//车辆购置价格 分
                'register_time' => $_os->register_time,//车辆登记日期
                'species' => $_os->species,//车辆种类
                'is_name' => $_os->is_name,//车辆种类 名字
                'owner' => $_os->owner,//车辆所有人
                'insurance_owner' => $_os->insurance_owner,//被保险人
                'identity_card' => $_os->identity_card,//身份证或组织机构代码
                'phone' => $_os->phone,//联系电话
                'operator_type' => $_os->operator_type,//操作员类型
                'operator_id' => $_os->operator_id,//操作员id
                'referrer_type' => $_os->referrer_type,//推荐人类型
                'referrer' => $_os->referrer,//推荐人id
                'referrer_name' => $referrer_name,//推荐人
                'accept_insurance_id' => $_os->accept_insurance_id,//承保公司
                'company_name_i' => $_os->company_name_i,//承保公司
                'bill_time' => $_os->bill_time,//开单日期
                'insurance_type' => $_os->insurance_type,//1交强险2商业险3交强与商业险
                'amount' => $_os->amount,//保险金额
                'car_damage' => isset($syx)?$syx->car_damage:0,//0无1 有
                'kickback_rate' => $_os->kickback_rate,//回扣比例
                'kickback_amount' => $_os->kickback_amount,//返现金额
                'is_renewal_insurance' => $_os->is_renewal_insurance,//新报续保
                'syx' => isset($syx)?$syx->policy_no:'',//返现金额
                'jqx' => isset($jqx)?$jqx->policy_no:'',//返现金额
                'syx_amount' => isset($syx)?$syx->amount:'',//商业险金额
                'jqx_amount' => isset($jqx)?$jqx->amount:'',//交强险
                'jqx_cc_amount' => isset($jqx)?$jqx->use_tax:'',//交强险车船使用税
                'syx_start' => isset($syx)?$syx->start_time:'',//商业险起包
                'jqx_start' => isset($jqx)?$jqx->start_time:'',//交强险起包
                'fx_syx_amount' => $_os->kickback_amount,//商业返现
                'fx_jqx_amount' => $_os->jq_amount,//交强返现
                'fx_ccx_amount' => $_os->cc_amount,//车船返现

            );
            array_push($insurance_detail,$_insurance_detaill);
        }
        return $insurance_detail;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-09-21 10:10:05
     * @Description: 企业订单报表
     */
    public static function get_enterprice_order_detail($begin_date,$end_date,$shop_id){
        $re_arr = [];
        $order = Orders::leftJoin('users', 'orders.pay_user_id', '=', 'users.id')->
        leftJoin('maintenance_shops', 'orders.shop_id', '=', 'maintenance_shops.id')
            ->whereIn('orders.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('orders.pay_time',array($begin_date,$end_date))
            ->where('orders.shop_id',$shop_id)
            ->where('orders.place',2)
            ->where('users.type',2)
            ->orderBy("orders.pay_time","desc")
            ->select(array(
                'orders.order_id',
                'users.phone',
                'orders.car_no',
                'orders.name',
                'users.type',
                'users.real_name',
                'orders.pay_time',
                'orders.user_car_id',
                'orders.amount',
                'orders.discount_amount',
                'orders.payable_amount',
                'orders.order_type',
                'orders.pay_type',
                'orders.verify_status',
                'orders.mileage',
                'orders.shop_id',
                'maintenance_shops.name as shop_name',
                'orders.status'
            ))
            ->get();
        foreach ($order as $_order) {
            $ro=ReturnOrder::where("order_id",$_order->order_id)->where("status",1)->first();
            $_order->return_amount=isset($ro)?$ro->amount:0;
            $order_pay_r=0;
            if($ro){
                $order_pay_r=ReturnOrderRecord::where("return_order_id",$ro->id)->where("pay_type",6)->sum("amount");
            }
            $order_pay=OrderPay::where("order_id",$_order->order_id)->where("status",1)->where("pay_type",6)->sum("amount");
            $_order->pay_details=$order_pay-$order_pay_r;
            $uci=UserCarInfo::leftJoin("anhui_data.car_standard_info","car_standard_info.id","=","user_car_info.car_model_id")
                ->where("user_car_info.id",$_order->user_car_id)->select(array(
                    "user_car_info.car_model_id",
                    "user_car_info.car_brand_id",
                    "user_car_info.car_series_id",
                    "car_standard_info.brand_name",
                    "car_standard_info.series_name",
                    "car_standard_info.model_name",
                    "user_car_info.filiale",
                    "user_car_info.department",
                ))->first();
            $_order->car_model_id=$uci->car_model_id;
            $_order->car_brand_id=$uci->car_brand_id;
            $_order->car_series_id=$uci->car_series_id;
            $_order->brand_name=$uci->brand_name;
            $_order->series_name=$uci->series_name;
            $_order->model_name=$uci->model_name;
            $_order->filiale=$uci->filiale;//分公司
            $_order->department=$uci->department;//部门
            $ucd=UserCarDesc::find($_order->user_car_id);
            $_order->dept_name=isset($ucd)?$ucd->dept_name:"";//单位名字
            $_order->cust_name=isset($ucd)?$ucd->name:"";//姓名
            $_order->cust_phone=isset($ucd)?$ucd->phone:"";//电话
            $_order->on_type=isset($ucd)?$ucd->on_type:"";//0 单 1双
            $_order->card_no=isset($ucd)?$ucd->card_no:"";//档案编号
            $_order->bookin_date=isset($ucd)?$ucd->bookin_date:"";//初登日期
            $t_order=Orders::whereIn('orders.status',array(1018,1017,1217,1317,1417))
                ->where('orders.user_car_id',$_order->user_car_id)->where('orders.pay_time',"<",$_order->pay_time)->orderBy("orders.pay_time","desc")->first();
            $_order->before_mileage=isset($t_order)?$t_order->mileage:"";//上次保养里程
            $re_arr[] = $_order;
        }
        //洗车单
        $wos = CarWashOrder::leftJoin('users', 'car_wash_order.user_id', '=', 'users.id')->
        leftJoin('maintenance_shops', 'car_wash_order.shop_id', '=', 'maintenance_shops.id')
            ->whereIn('car_wash_order.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('car_wash_order.pay_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->where('users.type',2)
            ->orderBy("car_wash_order.pay_time","desc")
            ->select(DB::raw(
                'car_wash_order.order_id,
                users.phone,
                car_wash_order.car_no,
                users.real_name as name,
                users.type,
                users.real_name,
                car_wash_order.pay_time,
                "" as user_car_id,
                car_wash_order.amount,
                car_wash_order.dis_amount as discount_amount,
                car_wash_order.pay_amount as payable_amount,
                "3" as order_type,
                car_wash_order.pay_type,              
                car_wash_order.verify_status,
                "/" as mileage,
                car_wash_order.shop_id,
                maintenance_shops.name as shop_name,
                car_wash_order.status'
            ))
            ->get();
        foreach ($wos as $wo) {
            $ro=ReturnCarwashOrder::where("order_id",$wo->order_id)->where("status",1)->first();
            $wo->return_amount=isset($ro)?$ro->amount:0;
            $order_pay_r=0;
            if($ro){
                $order_pay_r=ReturnWashOrderRecord::where("return_washorder_id",$ro->id)->where("pay_type",6)->sum("amount");
            }
            $order_pay=CarWashOrderPayRecord::where("order_id",$wo->order_id)->where("status",1)->where("pay_type",6)->sum("amount");
            $wo->pay_details=$order_pay-$order_pay_r;
            $wo->car_model_id='';
            $wo->car_brand_id='';
            $wo->car_series_id='';
            $wo->brand_name='/';
            $wo->series_name='/';
            $wo->model_name='/';
            $wo->filiale='/';//分公司
            $wo->department='/';//部门
            $wo->dept_name="/";//单位名字
            $wo->cust_name="/";//姓名
            $wo->cust_phone="/";//电话
            $wo->on_type=2;//0 单 1双
            $wo->card_no="/";//档案编号
            $wo->bookin_date="/";//初登日期
            $wo->before_mileage="/";//上次保养里程
            $re_arr[] = $wo;
        }
        //套餐卡单
        $scs = ServiceCardOrder::leftJoin('users', 'service_card_order.user_id', '=', 'users.id')->
        leftJoin('maintenance_shops', 'service_card_order.shop_id', '=', 'maintenance_shops.id')
            ->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('service_card_order.pay_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->where('users.type',2)
            ->orderBy("service_card_order.pay_time","desc")
            ->select(DB::raw(
                'service_card_order.order_id,
                users.phone,
                service_card_order.car_no,
                users.real_name as name,
                users.type,
                users.real_name,
                service_card_order.pay_time,
                "" as user_car_id,
                service_card_order.amount,
                "0" as discount_amount,
                service_card_order.pay_amount as payable_amount,
                "4" as order_type,
                service_card_order.pay_type,              
                service_card_order.verify_status,
                "/" as mileage,
                service_card_order.shop_id,
                maintenance_shops.name as shop_name,
                service_card_order.status'
            ))
            ->get();
        foreach ($scs as $sc) {
            $ro=ReturnServiceCarOrder::where("order_id",$sc->order_id)->where("status",1)->first();
            $sc->return_amount=isset($ro)?$ro->amount:0;
            $order_pay_r=0;
            if($ro){
                $order_pay_r=ReturnServiceCardRecord::where("return_order_id",$ro->id)->where("pay_type",6)->sum("amount");
            }
            $order_pay=ServiceCardOrderPayRecord::where("order_id", $sc->order_id)->where("status",1)->where("pay_type",6)->sum("amount");
            $sc->pay_details=$order_pay-$order_pay_r;
            $sc->car_model_id='';
            $sc->car_brand_id='';
            $sc->car_series_id='';
            $sc->brand_name='/';
            $sc->series_name='/';
            $sc->model_name='/';
            $sc->filiale='/';//分公司
            $sc->department='/';//部门
            $sc->dept_name="/";//单位名字
            $sc->cust_name="/";//姓名
            $sc->cust_phone="/";//电话
            $sc->on_type=2;//0单1双
            $sc->card_no="/";//档案编号
            $sc->bookin_date="/";//初登日期
            $sc->before_mileage="/";//上次保养里程
            $re_arr[] =  $sc;
        }
        return $re_arr;
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-08-26 11:20:42
     * @Description: 技师耗材业绩明细
     */
    public static function get_engineer_supply_detail($begin_date,$end_date,$shop_id){
        $order_product_detail = array();
        //获取订单
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('order_type',1)
            ->where('place',2)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach ($order as $_order){
            //获取订单下产品
            $order_product = OrderProduct::where('order_id',$_order->order_id)
                ->where('status',1)
                ->get();
            //获取服务产品包的三级分类
            $order_engineer = MaintenanceEngineers::find($_order->engineer_id);
            foreach ($order_product as $_order_product) {
                if($_order_product->product_type=='enterprise'){//说明是 企业报
                    $opep=OrderProductEsPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        if($_opep->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                        }
                        if(!$product_engineer){
                            continue;
                        }
                        $op_sypply=OrderProductEsItem::where("type",1)->where("order_product_package_id",$_opep->id)->get();
                        foreach ($op_sypply as $_os) {
                           // if($_os->item_type==0){
                           //     $item_info=SupplyItem::getCacheItemInfo($_os->item_id);
                           // }else{
                           //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_os->item_id);
                           // }
                            $item_info = SupplyItem::find($_os->item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            $_order_product_detail = array(
                                'order_id' => $_order->order_id,
                                'pay_time' => $_order->pay_time,
                                'car_no' => $_order->car_no,
                                'name' => $_order->name,
                                'engineer' => $product_engineer->name,
                                'engineer_id' => $product_engineer->id,
                                'item_id' => $_os->item_id,
                                'item_type' => $_os->item_type,
                                'item_brand_name' => $item_info->brand_name,
                                'item_series_name' => $item_info->series_name,
                                'item_model' => $item_info->model,
                                'item_code' => $item_info->code,
                               // 'item_specification' => $item_info->specification,
                               // 'item_bz_unit' => $item_info->bz_unit,
                                'item_pack' => $item_info->volume_size,
                               // 'item_unit' => $item_info->unit,
                                'si_name' => $item_info->name,
                                'all_amount' => round($_os->amount*$_os->num,2),
                                'counter_amount' => round($_os->counter_amount*$_os->num,2),
                                'number' => $_os->num,
                                'st_classifi_id' => $item_info->supply_classification_id,
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'reteurn_type' => 0,
                            );
                            array_push($order_product_detail,$_order_product_detail);
                        }
                    }
                }elseif($_order_product->product_type == 'activity'){//说明是 活动包
                    $opep=OrderProductAcPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        if($_opep->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                        }
                        if(!$product_engineer){
                            continue;
                        }
                        $op_sypply=OrderProductAcItem::where("type",1)->where("order_product_package_id",$_opep->id)->get();
                        foreach ($op_sypply as $_os) {
                            $item_info = SupplyItem::find($_os->item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            $_order_product_detail = array(
                                'order_id' => $_order->order_id,
                                'pay_time' => $_order->pay_time,
                                'car_no' => $_order->car_no,
                                'name' => $_order->name,
                                'engineer' => $product_engineer->name,
                                'engineer_id' => $product_engineer->id,
                                'item_id' => $_os->item_id,
                                'item_type' => $_os->item_type,
                                'item_brand_name' => $item_info->brand_name,
                                'item_series_name' => $item_info->series_name,
                                'item_model' => $item_info->model,
                                'item_code' => $item_info->code,
                               // 'item_specification' => $item_info->specification,
                               // 'item_bz_unit' => $item_info->bz_unit,
                                'item_pack' => $item_info->volume_size,
                               // 'item_unit' => $item_info->unit,
                                'si_name' => $item_info->name,
                                'all_amount' => round($_os->amount*$_os->num,2),
                                'counter_amount' => round($_os->counter_amount*$_os->num,2),
                                'number' => $_os->num,
                                'st_classifi_id' => $item_info->st_classifi_id,
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'reteurn_type' => 0,
                            );
                            array_push($order_product_detail,$_order_product_detail);
                        }
                    }
                }else if($_order_product->product_type=='base'){
                    if($_order_product->engineer_id == 0){
                        $product_engineer = $order_engineer;
                    }else{
                        $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                    }
                    if(!$product_engineer){
                        continue;
                    }
                    $op_sypply = OrderProductBaseItem::where("type",1)->where("order_product_package_id",$_order_product->id)->get();
                    foreach ($op_sypply as $_os) {
                        $item_info = SupplyItem::find($_os->item_id);
                        $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                        $_order_product_detail = array(
                            'order_id' => $_order->order_id,
                            'pay_time' => $_order->pay_time,
                            'car_no' => $_order->car_no,
                            'name' => $_order->name,
                            'engineer' => $product_engineer->name,
                            'engineer_id' => $product_engineer->id,
                            'item_id' => $_os->item_id,
                            'item_type' => $_os->item_type,
                            'item_brand_name' => $item_info->brand_name,
                            'item_series_name' => $item_info->series_name,
                            'item_model' => $item_info->model,
                            'item_code' => $item_info->code,
                           // 'item_specification' => $item_info->specification,
                           // 'item_bz_unit' => $item_info->bz_unit,
                            'item_pack' => $item_info->volume_size,
                           // 'item_unit' => $item_info->unit,
                            'si_name' => $item_info->name,
                            'all_amount' => round($_os->amount*$_os->num,2),
                            'counter_amount' => round($_os->counter_amount*$_os->num,2),
                            'number' => $_os->num,
                            'st_classifi_id' => $item_info->st_classifi_id,
                            'level_1_id' => $part_classification['level_1_id'],
                            'level_1_name' => $part_classification['level_1_name'],
                            'level_2_id' => $part_classification['level_2_id'],
                            'level_2_name' => $part_classification['level_2_name'],
                            'level_3_id' => $part_classification['level_3_id'],
                            'level_3_name' => $part_classification['level_3_name'],
                            'reteurn_type' => 0,
                        );
                        array_push($order_product_detail,$_order_product_detail);
                    }
                }else if($_order_product->product_type=='non_standard'){
                    if($_order_product->engineer_id == 0){
                        $product_engineer = $order_engineer;
                    }else{
                        $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                    }
                    if(!$product_engineer){
                        continue;
                    }
                    $op_sypply = OrderProductNonStandard::where("service_type",1)->where("order_product_id",$_order_product->id)->get();
                    foreach ($op_sypply as $_os) {
                        $item_info = SupplyItem::find($_os->item_id);
                        $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                        $_order_product_detail = array(
                            'order_id' => $_order->order_id,
                            'pay_time' => $_order->pay_time,
                            'car_no' => $_order->car_no,
                            'name' => $_order->name,
                            'engineer' => $product_engineer->name,
                            'engineer_id' => $product_engineer->id,
                            'item_id' => $_os->item_id,
                            'item_type' => $_os->type,
                            'item_brand_name' => $item_info->brand_name,
                            'item_series_name' => $item_info->series_name,
                            'item_model' => $item_info->model,
                            'item_code' => $item_info->code,
                           // 'item_specification' => $item_info->specification,
                           // 'item_bz_unit' => $item_info->bz_unit,
                            'item_pack' => $item_info->volume_size,
                           // 'item_unit' => $item_info->unit,
                            'si_name' => $item_info->name,
                            'all_amount' => round($_os->amount*$_os->num,2),
                            'counter_amount' => round($_os->counter_amount*$_os->num,2),
                            'number' => $_os->num,
                            'st_classifi_id' => $item_info->st_classifi_id,
                            'level_1_id' => $part_classification['level_1_id'],
                            'level_1_name' => $part_classification['level_1_name'],
                            'level_2_id' => $part_classification['level_2_id'],
                            'level_2_name' => $part_classification['level_2_name'],
                            'level_3_id' => $part_classification['level_3_id'],
                            'level_3_name' => $part_classification['level_3_name'],
                            'reteurn_type' => 0,
                        );
                        array_push($order_product_detail,$_order_product_detail);
                    }
                }else if($_order_product->product_type=='service_card'){
                    if($_order_product->engineer_id == 0){
                        $product_engineer = $order_engineer;
                    }else{
                        $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                    }
                    if(!$product_engineer){
                        continue;
                    }
                    $op_sypply = OrderProductScItem::where("type",1)->where("order_product_id",$_order_product->id)->get();
                    foreach ($op_sypply as $_os) {
                        $item_info = SupplyItem::find($_os->item_id);
                        $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                        $_order_product_detail = array(
                            'order_id' => $_order->order_id,
                            'pay_time' => $_order->pay_time,
                            'car_no' => $_order->car_no,
                            'name' => $_order->name,
                            'engineer' => $product_engineer->name,
                            'engineer_id' => $product_engineer->id,
                            'item_id' => $_os->item_id,
                            'item_type' => $_os->item_type,
                            'item_brand_name' => $item_info->brand_name,
                            'item_series_name' => $item_info->series_name,
                            'item_model' => $item_info->model,
                            'item_code' => $item_info->code,
                           // 'item_specification' => $item_info->specification,
                           // 'item_bz_unit' => $item_info->bz_unit,
                            'item_pack' => $item_info->volume_size,
                           // 'item_unit' => $item_info->unit,
                            'si_name' => $item_info->name,
                            'all_amount' => round($_os->amount*$_os->num,2),
                            'counter_amount' => round($_os->counter_amount*$_os->num,2),
                            'number' => $_os->num,
                            'st_classifi_id' => $item_info->st_classifi_id,
                            'level_1_id' => $part_classification['level_1_id'],
                            'level_1_name' => $part_classification['level_1_name'],
                            'level_2_id' => $part_classification['level_2_id'],
                            'level_2_name' => $part_classification['level_2_name'],
                            'level_3_id' => $part_classification['level_3_id'],
                            'level_3_name' => $part_classification['level_3_name'],
                            'reteurn_type' => 0,
                        );
                        array_push($order_product_detail,$_order_product_detail);
                    }
                }
            }
        }

        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->where('orders.order_type',1)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id',"orders.*"))->orderBy('return_order.implement_time',"asc")->get();
        foreach ($ro as $_ro){
            $order_engineer = MaintenanceEngineers::find($_ro->engineer_id);
            $roi=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->where("type",3)->first();//说明是 均摊的退货
            if($roi){
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",3)->where("type",3)->get();
            }else{//查找退配件 和退配件金额的
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->whereIn("type",array(0,1,4))->get();//
            }
            $pacek_arr=array();
            foreach ($ori as $_roi) {
                if($_roi->type ==4) {
                    if($_roi->product_type=='enterprise'){//说明是 企业报整包推
                        $opep=OrderProductEsPackage::where("order_product_id",$_roi->order_product_id)->get();
                        foreach ($opep as $_opep){
                            if($_opep->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                            }
                            if(!$product_engineer){
                                continue;
                            }
                            $op_sypply=OrderProductEsItem::where("type",1)->where("order_product_package_id",$_opep->id)->get();
                            foreach ($op_sypply as $_os) {
                                $item_info = SupplyItem::find($_os->item_id);
                                $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                                $_order_product_detail = array(
                                    'order_id' => $_ro->order_id,
                                    'pay_time' => $_ro->pay_time,
                                    'car_no' => $_ro->car_no,
                                    'name' => $_ro->name,
                                    'engineer' => $product_engineer->name,
                                    'engineer_id' => $product_engineer->id,
                                    'item_id' => $_os->item_id,
                                    'item_type' => $_os->item_type,
                                    'item_brand_name' => $item_info->brand_name,
                                    'item_series_name' => $item_info->series_name,
                                    'item_model' => $item_info->model,
                                    'item_code' => $item_info->code,
                                   // 'item_specification' => $item_info->specification,
                                   // 'item_bz_unit' => $item_info->bz_unit,
                                    'item_pack' => $item_info->volume_size,
                                   // 'item_unit' => $item_info->unit,
                                    'si_name' => $item_info->name,
                                    'all_amount' => 0,
                                    'counter_amount' => -round($_os->counter_amount*$_os->num,2),
                                    'number' => -$_os->num,
                                    'st_classifi_id' => $item_info->st_classifi_id,
                                    'level_1_id' => $part_classification['level_1_id'],
                                    'level_1_name' => $part_classification['level_1_name'],
                                    'level_2_id' => $part_classification['level_2_id'],
                                    'level_2_name' => $part_classification['level_2_name'],
                                    'level_3_id' => $part_classification['level_3_id'],
                                    'level_3_name' => $part_classification['level_3_name'],
                                    'reteurn_type' => 0,
                                );
                                array_push($order_product_detail,$_order_product_detail);
                            }
                        }
                    }elseif($_roi->product_type == 'activity'){//说明是 活动包
                        $opep=OrderProductAcPackage::where("order_product_id",$_roi->order_product_id)->get();
                        foreach ($opep as $_opep){
                            if($_opep->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                            }
                            if(!$product_engineer){
                                continue;
                            }
                            $op_sypply=OrderProductAcItem::where("type",1)->where("order_product_package_id",$_opep->id)->get();
                            foreach ($op_sypply as $_os) {
                                $item_info = SupplyItem::find($_os->item_id);
                                $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                                $_order_product_detail = array(
                                    'order_id' => $_ro->order_id,
                                    'pay_time' => $_ro->pay_time,
                                    'car_no' => $_ro->car_no,
                                    'name' => $_ro->name,
                                    'engineer' => $product_engineer->name,
                                    'engineer_id' => $product_engineer->id,
                                    'item_id' => $_os->item_id,
                                    'item_type' => $_os->item_type,
                                    'item_brand_name' => $item_info->brand_name,
                                    'item_series_name' => $item_info->series_name,
                                    'item_model' => $item_info->model,
                                    'item_code' => $item_info->code,
                                   // 'item_specification' => $item_info->specification,
                                   // 'item_bz_unit' => $item_info->bz_unit,
                                    'item_pack' => $item_info->volume_size,
                                   // 'item_unit' => $item_info->unit,
                                    'si_name' => $item_info->name,
                                    'all_amount' => 0,
                                    'counter_amount' => -round($_os->counter_amount*$_os->num,2),
                                    'number' => -$_os->num,
                                    'st_classifi_id' => $item_info->st_classifi_id,
                                    'level_1_id' => $part_classification['level_1_id'],
                                    'level_1_name' => $part_classification['level_1_name'],
                                    'level_2_id' => $part_classification['level_2_id'],
                                    'level_2_name' => $part_classification['level_2_name'],
                                    'level_3_id' => $part_classification['level_3_id'],
                                    'level_3_name' => $part_classification['level_3_name'],
                                    'reteurn_type' => 0,
                                );
                                array_push($order_product_detail,$_order_product_detail);
                            }
                        }
                    }else if($_roi->product_type=='base'){
                        $_order_product = OrderProduct::find($_roi->order_product_id);
                        if($_order_product->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                        }
                        if(!$product_engineer){
                            continue;
                        }
                        $op_sypply = OrderProductBaseItem::where("type",1)->where("order_product_package_id",$_roi->order_product_id)->get();
                        foreach ($op_sypply as $_os) {
                            $item_info = SupplyItem::find($_os->item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            $_order_product_detail = array(
                                'order_id' => $_ro->order_id,
                                'pay_time' => $_ro->pay_time,
                                'car_no' => $_ro->car_no,
                                'name' => $_ro->name,
                                'engineer' => $product_engineer->name,
                                'engineer_id' => $product_engineer->id,
                                'item_id' => $_os->item_id,
                                'item_type' => $_os->item_type,
                                'item_brand_name' => $item_info->brand_name,
                                'item_series_name' => $item_info->series_name,
                                'item_model' => $item_info->model,
                                'item_code' => $item_info->code,
                               // 'item_specification' => $item_info->specification,
                               // 'item_bz_unit' => $item_info->bz_unit,
                                'item_pack' => $item_info->volume_size,
                               // 'item_unit' => $item_info->unit,
                                'si_name' => $item_info->name,
                                'all_amount' =>0,
                                'counter_amount' => -round($_os->counter_amount*$_os->num,2),
                                'number' => -$_os->num,
                                'st_classifi_id' => $item_info->st_classifi_id,
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'reteurn_type' => 0,
                            );
                            array_push($order_product_detail,$_order_product_detail);
                        }
                    }else if($_roi->product_type=='non_standard'){
                        $_order_product = OrderProduct::find($_roi->order_product_id);
                        if($_order_product->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                        }
                        if(!$product_engineer){
                            continue;
                        }
                        $op_sypply = OrderProductNonStandard::where("service_type",1)->where("order_product_id",$_order_product->id)->get();
                        foreach ($op_sypply as $_os) {
                            $item_info = SupplyItem::find($_os->item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            $_order_product_detail = array(
                                'order_id' => $_ro->order_id,
                                'pay_time' => $_ro->pay_time,
                                'car_no' => $_ro->car_no,
                                'name' => $_ro->name,
                                'engineer' => $product_engineer->name,
                                'engineer_id' => $product_engineer->id,
                                'item_id' => $_os->item_id,
                                'item_type' => $_os->type,
                                'item_brand_name' => $item_info->brand_name,
                                'item_series_name' => $item_info->series_name,
                                'item_model' => $item_info->model,
                                'item_code' => $item_info->code,
                               // 'item_specification' => $item_info->specification,
                               // 'item_bz_unit' => $item_info->bz_unit,
                                'item_pack' => $item_info->volume_size,
                               // 'item_unit' => $item_info->unit,
                                'si_name' => $item_info->name,
                                'all_amount' => 0,
                                'counter_amount' => -round($_os->counter_amount*$_os->num,2),
                                'number' => -$_os->num,
                                'st_classifi_id' => $item_info->st_classifi_id,
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'reteurn_type' => 0,
                            );
                            array_push($order_product_detail,$_order_product_detail);
                        }
                    }else if($_roi->product_type=='service_card'){
                        $_order_product = OrderProduct::find($_roi->order_product_id);
                        if($_order_product->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                        }
                        if(!$product_engineer){
                            continue;
                        }
                        $op_sypply = OrderProductScItem::where("type",1)->where("order_product_id",$_order_product->id)->get();
                        foreach ($op_sypply as $_os) {
                            $item_info = SupplyItem::find($_os->item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            $_order_product_detail = array(
                                'order_id' => $_ro->order_id,
                                'pay_time' => $_ro->pay_time,
                                'car_no' => $_ro->car_no,
                                'name' => $_ro->name,
                                'engineer' => $product_engineer->name,
                                'engineer_id' => $product_engineer->id,
                                'item_id' => $_os->item_id,
                                'item_type' => $_os->item_type,
                                'item_brand_name' => $item_info->brand_name,
                                'item_series_name' => $item_info->series_name,
                                'item_model' => $item_info->model,
                                'item_code' => $item_info->code,
                               // 'item_specification' => $item_info->specification,
                               // 'item_bz_unit' => $item_info->bz_unit,
                                'item_pack' => $item_info->volume_size,
                               // 'item_unit' => $item_info->unit,
                                'si_name' => $item_info->name,
                                'all_amount' => 0,
                                'counter_amount' => -round($_os->counter_amount*$_os->num,2),
                                'number' => -$_os->num,
                                'st_classifi_id' => $item_info->st_classifi_id,
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'reteurn_type' => 0,
                            );
                            array_push($order_product_detail,$_order_product_detail);
                        }
                    }
                }else{
                    $product_engineer_id=0;
                    switch ($_roi->product_type) {
                        case 'enterprise':
                            $order_product_item = OrderProductEsItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==2){
                                continue 2;
                            }
                            $op=OrderProductEsPackage::find($order_product_item->order_product_package_id);
                            $item_type=$order_product_item->item_type;
                            $item_id=$order_product_item->item_id;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'activity':
                            $order_product_item = OrderProductAcItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==2){
                                continue 2;
                            }
                            $op=OrderProductAcPackage::find($order_product_item->order_product_package_id);
                            $item_type=$order_product_item->item_type;
                            $item_id=$order_product_item->item_id;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'promotion':
                            break;
                        case 'base':
                            $order_product_item = OrderProductBaseItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==2){
                                continue 2;
                            }
                            $op=OrderProduct::find($order_product_item->order_product_package_id);
                            $item_type=$order_product_item->item_type;
                            $item_id=$order_product_item->item_id;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'non_standard':
                            $order_product_item = OrderProductNonStandard::find($_roi->order_product_item_id);
                            if($order_product_item->service_type==2){
                                continue 2;
                            }
                            $op=OrderProduct::find($order_product_item->order_product_id);
                            $item_type=$order_product_item->type;
                            $item_id=$order_product_item->item_id;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'service_card':
                            $order_product_item = OrderProductScItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==2){
                                continue 2;
                            }
                            $op=OrderProduct::find($order_product_item->order_product_id);
                            $item_type=$order_product_item->item_type;
                            $item_id=$order_product_item->item_id;
                            $product_engineer_id=$op->engineer_id;
                            break;
                    }
                    $item_info = SupplyItem::find($item_id);
                    $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                    if($product_engineer_id==0){
                        $product_engineer = $order_engineer;
                    }else{
                        $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                    }
                    if(!$product_engineer){
                        continue;
                    }
                    $_order_product_detail = array(
                        'order_id' => $_ro->order_id,
                        'pay_time' => $_ro->pay_time,
                        'car_no' => $_ro->car_no,
                        'name' => $_ro->name,
                        'engineer' => $product_engineer->name,
                        'engineer_id' => $product_engineer->id,
                        'item_id' => $item_id,
                        'item_type' => $item_type,
                        'item_brand_name' => $item_info->brand_name,
                        'item_series_name' => $item_info->series_name,
                        'item_model' => $item_info->model,
                        'item_code' => $item_info->code,
                       // 'item_specification' => $item_info->specification,
                       // 'item_bz_unit' => $item_info->bz_unit,
                        'item_pack' => $item_info->volume_size,
                       // 'item_unit' => $item_info->unit,
                        'si_name' => $item_info->name,
                        'all_amount' =>0,
                        'counter_amount' =>-$_roi->return_amount,
                        'number' => -$_roi->number,
                        'st_classifi_id' => $item_info->st_classifi_id,
                        'level_1_id' => $part_classification['level_1_id'],
                        'level_1_name' => $part_classification['level_1_name'],
                        'level_2_id' => $part_classification['level_2_id'],
                        'level_2_name' => $part_classification['level_2_name'],
                        'level_3_id' => $part_classification['level_3_id'],
                        'level_3_name' => $part_classification['level_3_name'],
                        'reteurn_type' => 0,
                    );
                    array_push($order_product_detail,$_order_product_detail);
                }
            }
        }
        return $order_product_detail;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-08-17 15:39:19
     * @Description: 退货报表的详情
     */
    public static function get_return_order_detail($begin_date,$end_date,$shop_id){
        $order_derate_detail = array();
        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")

            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->where('orders.order_type',1)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.*',"orders.car_no"))->orderBy("return_order.implement_time","asc")->get();

        $type_arr=array(
            '0'=>'退配件库存',
            '1'=>'退配件',
            '2'=>'退工时',
            '3'=>'均摊',
            '4'=>'退服务');
        $reason_arr=array(
            '0'=>'无理由退货',
            '1'=>'质量存疑',
            '2'=>'折扣存疑',
            '3'=>'政府');
        foreach ($ro as $_ro){
            $roi=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->where("type",3)->first();//说明是 均摊的退货
            if($roi){
                $_order_derate_detail = array(
                    'return_order_id' => $_ro->id,
                    'order_id' => $_ro->order_id,
                    'order_type_name' => $type_arr[$roi->type],
                    'order_type' => $roi->type,
                    'car_no' => $_ro->car_no,
                    'supply_name' => "",
                    'service_name' => "",
                    'retunr_number' =>"",
                    'retunr_amount' => $roi->return_amount,
                    'implement_time' => $_ro->implement_time,
                    'created_name' =>$_ro->created_name,
                    'approver_name' =>$_ro->approver_name,
                    'reason' => $_ro->reason,
                );
                array_push($order_derate_detail,$_order_derate_detail);
            }else{
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->get();//
                foreach ($ori as $_roi) {
                    if($_roi->type != 4) {
                        $supply_name = $service_name = "";
                        switch ($_roi->product_type) {
                            case 'enterprise':
                                $order_product_item = OrderProductEsItem::find($_roi->order_product_item_id);
                                if ($order_product_item->type == 1) {//peijian
                                   // if ($order_product_item->item_type == 0) {//说明是标准
                                   //     $item_info = SupplyItem::getCacheItemInfo($order_product_item->item_id);
                                   // } else {
                                   //     $item_info = SupplyItemNonStandard::getCacheItemInfo($order_product_item->item_id);
                                   // }
                                   // $supply_name = $item_info->brand_name . " " . $item_info->series_name . " " . $item_info->model . " " . $item_info->code . " " . $item_info->specification;
                                    $item_info = SupplyItem::find($order_product_item->item_id);
                                    if($item_info) {
                                        $supply_name = $item_info->name;
                                    }
                                } else {
                                    $service_name = $order_product_item->item_name;
                                }
                                break;
                            case 'activity':
                                $order_product_item = OrderProductAcItem::find($_roi->order_product_item_id);
                                if ($order_product_item->type == 1) {//peijian
                                   // if ($order_product_item->item_type == 0) {//说明是标准
                                   //     $item_info = SupplyItem::getCacheItemInfo($order_product_item->item_id);
                                   // } else {
                                   //     $item_info = SupplyItemNonStandard::getCacheItemInfo($order_product_item->item_id);
                                   // }
                                   // $supply_name = $item_info->brand_name . " " . $item_info->series_name . " " . $item_info->model . " " . $item_info->code . " " . $item_info->specification;
                                    $item_info = SupplyItem::find($order_product_item->item_id);
                                    if($item_info) {
                                        $supply_name = $item_info->name;
                                    }
                                } else {
                                    $service_name = $order_product_item->item_name;
                                }
                                break;
                            case 'promotion':
                                break;
                            case 'base':
                                $order_product_item = OrderProductBaseItem::find($_roi->order_product_item_id);
                                if ($order_product_item->type == 1) {
                                   // if ($order_product_item->item_type == 0) {//说明是标准
                                   //     $item_info = SupplyItem::getCacheItemInfo($order_product_item->item_id);
                                   // } else {
                                   //     $item_info = SupplyItemNonStandard::getCacheItemInfo($order_product_item->item_id);
                                   // }
                                   // $supply_name = $item_info->brand_name . " " . $item_info->series_name . " " . $item_info->model . " " . $item_info->code . " " . $item_info->specification;
                                    $item_info = SupplyItem::find($order_product_item->item_id);
                                    if($item_info) {
                                        $supply_name = $item_info->name;
                                    }
                                } else {
                                    $service_name = $order_product_item->item_name;
                                }
                                break;
                            case 'non_standard':
                                $order_product_item = OrderProductNonStandard::find($_roi->order_product_item_id);
                                if ($order_product_item->service_type == 1) {
                                   // if ($order_product_item->type == 0) {//说明是标准
                                   //     $item_info = SupplyItem::getCacheItemInfo($order_product_item->item_id);
                                   // } else {
                                   //     $item_info = SupplyItemNonStandard::getCacheItemInfo($order_product_item->item_id);
                                   // }
                                   // $supply_name = $item_info->brand_name . " " . $item_info->series_name . " " . $item_info->model . " " . $item_info->code . " " . $item_info->specification;
                                    $item_info = SupplyItem::find($order_product_item->item_id);
                                    if($item_info) {
                                        $supply_name = $item_info->name;
                                    }
                                } else {
                                    $service_name = $order_product_item->name;
                                }
                                break;
                            case 'service_card':
                                $order_product_item = OrderProductScItem::find($_roi->order_product_item_id);
                                if($order_product_item) {
                                    if ($order_product_item->type == 1) {
                                       // if ($order_product_item->item_type == 0) {//说明是标准
                                       //     $item_info = SupplyItem::getCacheItemInfo($order_product_item->item_id);
                                       // } else {
                                       //     $item_info = SupplyItemNonStandard::getCacheItemInfo($order_product_item->item_id);
                                       // }
                                       // $supply_name = $item_info->brand_name . " " . $item_info->series_name . " " . $item_info->model . " " . $item_info->code . " " . $item_info->specification;
                                        $item_info = SupplyItem::find($order_product_item->item_id);
                                        if($item_info) {
                                            $supply_name = $item_info->name;
                                        }
                                    } else {
                                        $service_name = $order_product_item->name;
                                    }
                                }
                                break;
                        }
                        $_order_derate_detail = array(
                            'return_order_id' => $_ro->id,
                            'order_id' => $_ro->order_id,
                            'order_type_name' => $type_arr[$_roi->type],
                            'order_type' => $_roi->type,
                            'car_no' => $_ro->car_no,
                            'supply_name' => $supply_name,
                            'service_name' => $service_name,
                            'retunr_number' => $_roi->number,
                            'retunr_amount' => $_roi->return_amount,
                            'implement_time' => $_ro->implement_time,
                            'created_name' => $_ro->created_name,
                            'approver_name' => $_ro->approver_name,
                            'reason' => $_ro->reason,
                        );
                        array_push($order_derate_detail, $_order_derate_detail);
                    }else{
                        $op = OrderProduct::find($_roi->order_product_id);
                        if($op->product_type=='enterprise'){//说明是 企业报
                            $opep=OrderProductEsPackage::where("order_product_id",$_roi->order_product_id)->get();
                            foreach ($opep as $_opep){
                                $item = OrderProductEsItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                                $order_derate_detail = Report::get_supply_items($_ro,$_roi,$item,$type_arr,$order_derate_detail);
                            }
                        }elseif($op->product_type=='activity'){//说明是 企业报
                            $opep=OrderProductAcPackage::where("order_product_id",$_roi->order_product_id)->get();
                            foreach ($opep as $_opep){
                                $item = OrderProductAcItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                                $order_derate_detail = Report::get_supply_items($_ro,$_roi,$item,$type_arr,$order_derate_detail);
                            }
                        }elseif($op->product_type=='base'){//说明是 基础
                            $item = OrderProductBaseItem::where("order_product_package_id",$_roi->order_product_id)->get();
                            $order_derate_detail = Report::get_supply_items($_ro,$_roi,$item,$type_arr,$order_derate_detail);
                        }elseif($op->product_type=='non_standard'){//说明是 非标
                            $item = OrderProductNonStandard::where("order_product_id",$_roi->order_product_id)->get();
                            if(!is_null($item)) {
                                foreach ($item as $_item) {
                                    $supply_name = $service_name = "";
                                    $return_amount=0;
                                    if ($_item->service_type == 1) {//peijian
                                       // if ($_item->item_type == 0) {//说明是标准
                                       //     $item_info = SupplyItem::getCacheItemInfo($_item->item_id);
                                       // } else {
                                       //     $item_info = SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                       // }
                                       // $supply_name = $item_info->brand_name . " " . $item_info->series_name . " " . $item_info->model . " " . $item_info->code . " " . $item_info->specification;
                                        $item_info = SupplyItem::find($_item->item_id);
                                        if($item_info) {
                                            $supply_name = $item_info->name;
                                        }
                                        $return_amount=$_item->counter_amount*$_item->num;
                                    } else {
                                        $service_name = $_item->item_name;
                                        $return_amount=$_item->counter_amount;
                                    }
                                    $_order_product_detail = array(
                                        'return_order_id' => $_ro->id,
                                        'order_id' => $_ro->order_id,
                                        'order_type_name' => $type_arr[$_roi->type],
                                        'order_type' => $_roi->type,
                                        'car_no' => $_ro->car_no,
                                        'supply_name' => $supply_name,
                                        'service_name' => $service_name,
                                        'retunr_number' => $_item->num,
                                        'retunr_amount' => $return_amount,
                                        'implement_time' => $_ro->implement_time,
                                        'created_name' => $_ro->created_name,
                                        'approver_name' => $_ro->approver_name,
                                        'reason' => $_ro->reason,
                                    );
                                    array_push($order_derate_detail,$_order_product_detail);
                                }
                            }
                        }elseif($op->product_type=='service_card'){//说明是 套餐卡
                            $item = OrderProductScItem::where("order_product_id",$_roi->order_product_id)->get();
                            $order_derate_detail = Report::get_supply_items($_ro,$_roi,$item,$type_arr,$order_derate_detail);
                        }
                    }
                }
            }
        }
        //洗车单退货
        $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->select(array('return_carwash_order.*',"car_wash_order.car_no","car_wash_order.producd_id"))
            ->get();
        foreach($wash_os_r as $wo_r){
            $pp = ProductPackage::find($wo_r->producd_id);
            $_order_derate_detail_1 = array(
                'return_order_id' => $wo_r->id,
                'order_id' => $wo_r->order_id,
                'order_type_name' => '退工时',
                'order_type' => 2,
                'car_no' => $wo_r->car_no,
                'supply_name' => '',
                'service_name' => $pp->name,
                'retunr_number' =>0,
                'retunr_amount' => $wo_r->amount,
                'implement_time' => $wo_r->implement_time,
                'created_name' =>$wo_r->created_name,
                'approver_name' =>$wo_r->approver_name,
                'reason' => $wo_r->reason,
            );
            array_push($order_derate_detail,$_order_derate_detail_1);
        }

        //套餐卡退货
        $wash_os_r = ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")
            ->where('return_service_card_order.status',1)
            ->whereBetween('return_service_card_order.implement_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->select(array('return_service_card_order.*',"service_card_order.car_no","service_card_order.service_card_id"))
            ->get();
        foreach($wash_os_r as $wo_r){
            $pp = ServiceCardBase::find($wo_r->service_card_id);
            $_order_derate_detail_1 = array(
                'return_order_id' => $wo_r->id,
                'order_id' => $wo_r->order_id,
                'order_type_name' => '退服务',
                'order_type' => 4,
                'car_no' => $wo_r->car_no,
                'supply_name' => '',
                'service_name' => $pp->name,
                'retunr_number' =>0,
                'retunr_amount' => $wo_r->amount,
                'implement_time' => $wo_r->implement_time,
                'created_name' =>$wo_r->created_name,
                'approver_name' =>$wo_r->approver_name,
                'reason' => $wo_r->reason,
            );
            array_push($order_derate_detail,$_order_derate_detail_1);
        }
        return  $order_derate_detail;
    }
    /**
     * @Author:      lyt
     * @DateTime:    2016-07-20 09:32:40
     * @Description: 未结算订单详情
     */

    public static function temp_get_order_detail($shop_id){
        $order_derate_detail = array();
        $order = Orders::whereIn('status',array(1216,1416,1215))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            $user = Users::where('id',$_order->user_id)->first();
            $user_car_info = DataCarStandardInfo::find($_order->car_model_id);
            if($_order->order_type == 1) {
                $order_type = '服务';
            }elseif($_order->order_type == 2){
                $order_type = '零售';
            }else{
                $order_type = '未知';
            }
            if($_order->place == 1) {
                $order_place = '上门';
            }elseif($_order->place == 2){
                $order_place = '到店';
            }else{
                $order_place = '未知';
            }
            if($user->type == 1) {
                $user_type = '个人';
            }elseif($user->type == 2){
                $user_type = '企业';
            }else{
                $user_type = '未知';
            }
            if(is_null($_order->discount_amount)){
                $discount_amount = 0;
            }else{
                $discount_amount = $_order->discount_amount;
            }
            $_order_derate_detail = array(
                'data_type' => 'normal_order',
                'order_id' => $_order->order_id,
                'order_type' => $order_type,
                'order_place' => $order_place,
                'user_type' => $user_type,
                'user_name' => $_order->name,
                'car_no' => $_order->car_no,
                'car_brand_name' => isset($user_car_info)?$user_car_info->brand_name:'',
                'car_series_name' => isset($user_car_info)?$user_car_info->series_name:'',
                'car_model_name' => isset($user_car_info)?$user_car_info->model_name:'',
                'car_year_name' => isset($user_car_info)?$user_car_info->model_year:'',
                'car_model_gas_rate' => isset($user_car_info)?$user_car_info->model_gas_rate:'',
                'create_time' => $_order->created_at."",
                'pay_time' => $_order->pay_time,
                'amount' => $_order->amount,
                'discount_rate' => $_order->discount_rate,
                'discount_amount' => $_order->discount,
                'coupon_amount' => $discount_amount,
                'balance_amount' => $_order->balance_amount,
                'payable_amount' => $_order->payable_amount,
                'disrate_amount' => $_order->amount - $_order->discount - $discount_amount - $_order->payable_amount ,
                'note' => $_order->note,
            );
            array_push($order_derate_detail,$_order_derate_detail);
        }
        //洗车单
        $wash_os = CarWashOrder::where('status',1411)
            ->where('shop_id',$shop_id)
            ->get();
        foreach($wash_os as $wo){
            if(is_null($wo->amount)){
                $amount = 0;
            }else{
                $amount = $wo->amount;
            }
            if(is_null($wo->pay_amount)){
                $pay_amount = 0;
            }else{
                $pay_amount = $wo->pay_amount;
            }
            $_order_derate_detail_1 = array(
                'data_type' => 'wash_order',
                'order_id' => $wo->order_id,
                'order_type' => '洗车',
                'order_place' => '到店',
                'user_type' => '个人',
                'user_name' => '',
                'car_no' => $wo->car_no,
                'car_brand_name' => '',
                'car_series_name' => '',
                'car_model_name' => '',
                'car_year_name' => '',
                'car_model_gas_rate' => '',
                'create_time' => $wo->created_at . "",
                'pay_time' => $wo->pay_time,
                'amount' => $amount,
                'discount_rate' => 1,
                'discount_amount' => $amount - $pay_amount,
                'coupon_amount' => $amount - $pay_amount,
                'balance_amount' => $amount,
                'payable_amount' => $pay_amount,
                'disrate_amount' => $amount - $pay_amount,
                'note' => '',
            );
            array_push($order_derate_detail,$_order_derate_detail_1);
        }

        //套餐卡订单
        $wash_os = ServiceCardOrder::where('status',1411)
            ->where('shop_id',$shop_id)
            ->get();
        foreach($wash_os as $wo){
            if(is_null($wo->amount)){
                $amount = 0;
            }else{
                $amount = $wo->amount;
            }
            if(is_null($wo->pay_amount)){
                $pay_amount = 0;
            }else{
                $pay_amount = $wo->pay_amount;
            }
            $_order_derate_detail_1 = array(
                'data_type' => 'service_card_order',
                'order_id' => $wo->order_id,
                'order_type' => '套餐卡订单',
                'order_place' => '到店',
                'user_type' => '个人',
                'user_name' => '',
                'car_no' => $wo->car_no,
                'car_brand_name' => '',
                'car_series_name' => '',
                'car_model_name' => '',
                'car_year_name' => '',
                'car_model_gas_rate' => '',
                'create_time' => $wo->created_at . "",
                'pay_time' => $wo->pay_time,
                'amount' => $amount,
                'discount_rate' => 1,
                'discount_amount' => $amount - $pay_amount,
                'coupon_amount' => $amount - $pay_amount,
                'balance_amount' => $amount,
                'payable_amount' => $pay_amount,
                'disrate_amount' => $amount - $pay_amount,
                'note' => '',
            );
            array_push($order_derate_detail,$_order_derate_detail_1);
        }
        return  $order_derate_detail;
    }

    //订单减免详情
    public static function get_order_derate_detail($begin_date,$end_date,$shop_id){
        $order_derate_detail = array();
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            $user = Users::where('id',$_order->user_id)->first();
            $shop = MaintenanceShops::where('id',$_order->shop_id)->first();
            if($_order->order_type == 1) {
                $order_type = '服务';
            }elseif($_order->order_type == 2){
                $order_type = '零售';
            }else{
                $order_type = '未知';
            }
            if($_order->place == 1) {
                $order_place = '上门';
            }elseif($_order->place == 2){
                $order_place = '到店';
            }else{
                $order_place = '未知';
            }
            if($user->type == 1) {
                $user_type = '个人';
            }elseif($user->type == 2){
                $user_type = '企业';
            }else{
                $user_type = '未知';
            }
            if(is_null($_order->discount_amount)){
                $discount_amount = 0;
            }else{
                $discount_amount = $_order->discount_amount;
            }
            $_order_derate_detail = array(
                'shop' => $shop->name,
                'order_id' => $_order->order_id,
                'order_type' => $order_type,
                'order_place' => $order_place,
                'user_type' => $user_type,
                'user_name' => $_order->name,
                'car_no' => $_order->car_no,
                'create_time' => $_order->created_at,
                'pay_time' => $_order->pay_time,
                'amount' => $_order->amount,
                'discount_rate' => $_order->discount_rate,
                'discount_amount' => $_order->discount,
                'coupon_amount' => $discount_amount,
                'balance_amount' => $_order->balance_amount,
                'payable_amount' => $_order->payable_amount,
                'disrate_amount' => $_order->amount - $_order->discount - $discount_amount - $_order->payable_amount ,
                'note' => $_order->note,
            );
            array_push($order_derate_detail,$_order_derate_detail);
        }
        //洗车单
        $wash_os = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach($wash_os as $wo){
            $sho = MaintenanceShops::where('id',$wo->shop_id)->first();
            $_order_derate_detail = array(
                'shop' => $sho->name,
                'data_type' => 'wash_order',
                'order_id' => $wo->order_id,
                'order_type' => '洗车',
                'order_place' => '到店',
                'user_type' => '个人',
                'user_name' => '',
                'car_no' => $wo->car_no,
                'create_time' => $wo->created_at,
                'pay_time' => $wo->pay_time,
                'amount' => $wo->amount,
                'discount_rate' => 1,
                'discount_amount' => 0,
                'coupon_amount' => $wo->dis_amount,
                'balance_amount' => $wo->amount,
                'payable_amount' => $wo->pay_amount,
                'disrate_amount' => $wo->amount - $wo->pay_amount ,
                'note' => '',
            );
            array_push($order_derate_detail,$_order_derate_detail);
        }
        //套餐卡订单
        $wash_os = ServiceCardOrder::where('status',1417)
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach($wash_os as $wo){
            $sho = MaintenanceShops::where('id',$wo->shop_id)->first();
            $_order_derate_detail = array(
                'shop' => $sho->name,
                'data_type' => 'service_card_order',
                'order_id' => $wo->order_id,
                'order_type' => '套餐卡订单',
                'order_place' => '到店',
                'user_type' => '个人',
                'user_name' => '',
                'car_no' => $wo->car_no,
                'create_time' => $wo->created_at,
                'pay_time' => $wo->pay_time,
                'amount' => $wo->amount,
                'discount_rate' => 1,
                'discount_amount' => 0,
                'coupon_amount' => 0,
                'balance_amount' => $wo->amount,
                'payable_amount' => $wo->pay_amount,
                'disrate_amount' => $wo->amount - $wo->pay_amount ,
                'note' => '',
            );
            array_push($order_derate_detail,$_order_derate_detail);
        }
        return $order_derate_detail;
    }

    //新版进销存
    /**
     * @Author:      lyt
     * @DateTime:    2016-07-29 16:17:00
     * @Description: 废弃 data_id 现统一传0
     */
    public static function get_supply_stock_new($begin_date,$end_date,$data_id,$shop_id){
        $finally=array();
        $begin_date_new=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date_new=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));
        //获取 该库的期初库存
        $start_stock_array=array();
        // $start_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($begin_date_new)))->where("data_id",$data_id)->get();
        $start_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($begin_date_new)-86400))->select("item_type","item_id","number","amount")->get();
        foreach ($start_stock as $_start_stock) {
            if(isset($start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id])){
                $temp=$start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id];
                $temp->number=$temp->number+$_start_stock->number;
                $temp->amount=$temp->amount+$_start_stock->amount;
                $start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id]=$temp;
            }else{
                $start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id]=$_start_stock;
            }

        }
        unset($start_stock);
        // //入库数据
        // $storage_data = Report::get_storage_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
        // // var_dump($storage_data);exit;
        // //服务订单维修出库
        // $service_order_out_data = Report::get_service_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        // //零售订单销售出库
        // $sale_order_out_data = Report::get_sale_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        // //门店间内部调出出库
        // $transfer_out_data = Report::get_transfer_out_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
        // /*供货商退货出库*/
        // $supplier_out_data = Report::get_supplier_out_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
        // //盘点盘盈调整入库
        // $inventory_storage_data = Report::get_inventory_storage_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
        // //盘点盘亏调整出库
        // $inventory_out_data = Report::get_inventory_out_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
        // //结算后退货的
        // $return_stireage_date = Report::get_return_storage_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);


        //入库数据
        $storage_data = ReportDateReocrd::get_storage_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        // var_dump($storage_data);exit;
        //服务订单维修出库
        $service_order_out_data = ReportDateReocrd::get_service_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        //零售订单销售出库
        $sale_order_out_data = ReportDateReocrd::get_sale_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        //门店间内部
        $transfer_out_data = ReportDateReocrd::get_transfer_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);

        //门店间内部领用领用
        $transfer_out_data_ly = ReportDateReocrd::get_transfer_out_data_for_jxc_ly($shop_id,$data_id,$begin_date,$end_date);
        //门店间内部调出出库
        $transfer_out_data_dc = ReportDateReocrd::get_transfer_out_data_for_jxc_dc($shop_id,$data_id,$begin_date,$end_date);
        //门店间内部调出出库
        $transfer_out_data_xs = ReportDateReocrd::get_transfer_out_data_for_jxc_xs($shop_id,$data_id,$begin_date,$end_date);

        /*供货商退货出库*/
        $supplier_out_data = ReportDateReocrd::get_supplier_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        //盘点盘盈调整入库
        $inventory_storage_data = ReportDateReocrd::get_inventory_storage_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        //盘点盘亏调整出库
        $inventory_out_data = ReportDateReocrd::get_inventory_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
        //结算后退货的
        $return_stireage_date = ReportDateReocrd::get_return_storage_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);

        //获取 该库的期末库存
        $end_stock_array=array();
        // $end_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($end_date_new)))->where("data_id",$data_id)->get();
        $end_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($end_date_new)))->select("item_type","item_id","number","amount")->get();
        foreach ($end_stock as $_end_stock) {
            if(isset($end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id])){
                $temp=$end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id];
                $temp->number=$temp->number+$_end_stock->number;
                $temp->amount=$temp->amount+$_end_stock->amount;
                $end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id]=$temp;
            }else{
                $end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id]=$_end_stock;
            }
        }
        unset($end_stock);

        $maintenance_ware_house = MaintenanceWarehouse::leftJoin("supply_item","supply_item.id","=","maintenance_warehouse.item_id")->where("maintenance_warehouse.shop_id",$shop_id)->where('maintenance_warehouse.created_at','<=',$end_date)->select("maintenance_warehouse.item_type","maintenance_warehouse.item_id","supply_item.brand_name","supply_item.series_name","supply_item.model","supply_item.code","supply_item.volume_size","supply_item.name","supply_item.supply_classification_id")->get();
        foreach ($maintenance_ware_house as $_maintenance_ware_house){
            $key=$_maintenance_ware_house->item_type.'-'.$_maintenance_ware_house->item_id;
           //  获取耗材信息
           // if($_maintenance_ware_house->item_type==0){
           //     $item_info=SupplyItem::getCacheItemInfo($_maintenance_ware_house->item_id);
           // }else{
           //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_maintenance_ware_house->item_id);
           // }
           // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
            // $item_info = SupplyItem::find($_maintenance_ware_house->item_id);
            $part_classification = Report::get_part_classification($_maintenance_ware_house->supply_classification_id);
            $start_number=isset($start_stock_array[$key]->number)?$start_stock_array[$key]->number:0;
            $start_amount=isset($start_stock_array[$key]->amount)?$start_stock_array[$key]->amount:0;
            $duo=isset($inventory_storage_data[$key]['number'])?$inventory_storage_data[$key]['number']:0;
            $duo_amount=isset($inventory_storage_data[$key]['tax_purchase_subtotal'])?$inventory_storage_data[$key]['tax_purchase_subtotal']:0;

            $shao=isset($inventory_out_data[$key]['number'])?$inventory_out_data[$key]['number']:0;
            $shao_amount=isset($inventory_out_data[$key]['tax_purchase_subtotal'])?$inventory_out_data[$key]['tax_purchase_subtotal']:0;

            //获取 时间段内的入库成本
            $temp_array=array(
                'item_type' => $_maintenance_ware_house->item_type,
                'item_id' => $_maintenance_ware_house->item_id,
                'item_brand_name' => $_maintenance_ware_house->brand_name,
                'item_series_name' => $_maintenance_ware_house->series_name,
                'item_model' => $_maintenance_ware_house->model,
                'item_code' => $_maintenance_ware_house->code,
               // 'item_specification' => $item_info->specification,
               // 'item_bz_unit' => $item_info->bz_unit,
                'item_pack' => $_maintenance_ware_house->volume_size,
                'item_name' => $_maintenance_ware_house->name,
               // 'item_unit' => $item_info->unit,
                'st_classifi_id' => $_maintenance_ware_house->supply_classification_id,
                'level_1_id' => $part_classification['level_1_id'],
                'level_1_name' => $part_classification['level_1_name'],
                'level_2_id' => $part_classification['level_2_id'],
                'level_2_name' => $part_classification['level_2_name'],
                'level_3_id' => $part_classification['level_3_id'],
                'level_3_name' => $part_classification['level_3_name'],
                'start_stock' => $start_number,
                'start_amount' =>$start_amount,
                'into_stock' => isset($storage_data[$key]['number'])?$storage_data[$key]['number']:0,
                'into_amount' => isset($storage_data[$key]['tax_purchase_subtotal'])?$storage_data[$key]['tax_purchase_subtotal']:0,
                'return_into_stock' => isset($return_stireage_date[$key]['number'])?$return_stireage_date[$key]['number']:0,
                'return_into_amount' => isset($return_stireage_date[$key]['tax_purchase_subtotal'])?$return_stireage_date[$key]['tax_purchase_subtotal']:0,
                'orderout_stock' => isset($service_order_out_data[$key]['number'])?$service_order_out_data[$key]['number']:0,
                'orderout_amount' => isset($service_order_out_data[$key]['tax_purchase_subtotal'])?$service_order_out_data[$key]['tax_purchase_subtotal']:0,
                'saleout_stock' => isset($sale_order_out_data[$key]['number'])?$sale_order_out_data[$key]['number']:0,
                'saleout_amount' => isset($sale_order_out_data[$key]['tax_purchase_subtotal'])?$sale_order_out_data[$key]['tax_purchase_subtotal']:0,
                'diaochu_stock' => isset($transfer_out_data[$key]['number'])?$transfer_out_data[$key]['number']:0,
                'diaochu_amount' => isset($transfer_out_data[$key]['tax_purchase_subtotal'])?$transfer_out_data[$key]['tax_purchase_subtotal']:0,

                'diaochu_stock_ly' => isset($transfer_out_data_ly[$key]['number'])?$transfer_out_data_ly[$key]['number']:0,
                'diaochu_amount_ly' => isset($transfer_out_data_ly[$key]['tax_purchase_subtotal'])?$transfer_out_data_ly[$key]['tax_purchase_subtotal']:0,
                'diaochu_stock_dc' => isset($transfer_out_data_dc[$key]['number'])?$transfer_out_data_dc[$key]['number']:0,
                'diaochu_amount_dc' => isset($transfer_out_data_dc[$key]['tax_purchase_subtotal'])?$transfer_out_data_dc[$key]['tax_purchase_subtotal']:0,
                'diaochu_stock_xs' => isset($transfer_out_data_xs[$key]['number'])?$transfer_out_data_xs[$key]['number']:0,
                'diaochu_amount_xs' => isset($transfer_out_data_xs[$key]['tax_purchase_subtotal'])?$transfer_out_data_xs[$key]['tax_purchase_subtotal']:0,

                'pandian_stock' => $duo-$shao,
                'pandian_amount' => $duo_amount-$shao_amount,
                'supplier_stock' => isset($supplier_out_data[$key]['number'])?$supplier_out_data[$key]['number']:0,
                'supplier_amount' => isset($supplier_out_data[$key]['tax_purchase_subtotal'])?$supplier_out_data[$key]['tax_purchase_subtotal']:0,
                'end_stock' => isset($end_stock_array[$key]->number)?$end_stock_array[$key]->number:0,
                'end_amount' => isset($end_stock_array[$key]->amount)?$end_stock_array[$key]->amount:0,
            );
            // $temp_array["end_stock"]=$temp_array["start_stock"]+$temp_array["into_stock"]-$temp_array["orderout_stock"]-$temp_array["saleout_stock"]-$temp_array["diaochu_stock"]+$temp_array["pandian_stock"]-$temp_array["supplier_stock"];
            // $temp_array["end_amount"]=$temp_array["start_amount"]+$temp_array["into_amount"]-$temp_array["orderout_amount"]-$temp_array["saleout_amount"]-$temp_array["diaochu_amount"]+$temp_array["pandian_amount"]-$temp_array["supplier_amount"];
            $finally[]=$temp_array;
        }        
        unset($maintenance_ware_house);
        unset($end_stock_array);
        unset($return_stireage_date);
        unset($inventory_out_data);
        unset($inventory_storage_data);
        unset($supplier_out_data);
        unset($transfer_out_data_xs);
        unset($transfer_out_data_dc);
        unset($transfer_out_data_ly);
        unset($transfer_out_data);
        unset($sale_order_out_data);
        unset($service_order_out_data);
        unset($storage_data);
        unset($start_stock_array);        
        return $finally;
    }

    /*结算后退库的耗材*/
    public static function get_return_storage_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $storage_item = array();
        $maintenance_storage_item =MaintenanceStorageItem::leftJoin("maintenance_return_order","maintenance_return_order.id","=","maintenance_storage_item.return_order_id")->
        leftJoin("maintenance_out_item","maintenance_out_item.id","=","maintenance_storage_item.out_id")
            ->where('maintenance_return_order.source','return_order')
            ->whereBetween('maintenance_storage_item.implement_time',array($begin_date,$end_date))
            ->where('maintenance_return_order.shop_id',$shop)
            ->where('maintenance_return_order.status',1)
            ->select(array("maintenance_storage_item.*","maintenance_out_item.tax_purchase_price as temp_tax_purchase_price"))
            ->get();
        foreach ($maintenance_storage_item as $_maintenance_storage_item){
            $_storage_item = array(
                'code' => $_maintenance_storage_item->code,
                'name' => $_maintenance_storage_item->name,
                'data_id' => $_maintenance_storage_item->data_id,
                'item_id' =>  $_maintenance_storage_item->item_id,
                'item_type' =>  $_maintenance_storage_item->item_type,
                'number' => $_maintenance_storage_item->number,
                'tax_purchase_price' => $_maintenance_storage_item->temp_tax_purchase_price,
                'tax_purchase_subtotal' => $_maintenance_storage_item->number * $_maintenance_storage_item->temp_tax_purchase_price,
            );
            $key = $_maintenance_storage_item->item_type . '-' . $_maintenance_storage_item->item_id;
            if(isset($storage_item[$key])){
                $storage_item[$key]['number'] = $storage_item[$key]['number'] + $_storage_item['number'];
                $storage_item[$key]['tax_purchase_subtotal'] = $storage_item[$key]['tax_purchase_subtotal'] + $_storage_item['tax_purchase_subtotal'];
            }else{
                $storage_item[$key] = $_storage_item;
            }
        }
        return $storage_item;
    }


    /*入库数据*/
    public static function get_storage_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $storage_item = array();
        $maintenance_storage_list = MaintenanceStorageList::where('shop_id',$shop)
            ->where('status',1)
            ->whereIn('type',array(1,2)) //需要明确入库类型
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->get();
        foreach ($maintenance_storage_list as $_maintenance_storage_list){
            $maintenance_storage_item = MaintenanceStorageItem::where('list_id',$_maintenance_storage_list->id)
                // ->where('data_id',$data_id)
                ->where('status',1)
                ->get();
            foreach ($maintenance_storage_item as $_maintenance_storage_item){
                $_storage_item = array(
                    'code' => $_maintenance_storage_item->code,
                    'name' => $_maintenance_storage_item->name,
                    'data_id' => $_maintenance_storage_item->data_id,
                    'item_id' =>  $_maintenance_storage_item->item_id,
                    'item_type' =>  $_maintenance_storage_item->item_type,
                    'number' => $_maintenance_storage_item->number,
                    'tax_purchase_price' => $_maintenance_storage_item->tax_purchase_price,
                    'tax_purchase_subtotal' => $_maintenance_storage_item->number * $_maintenance_storage_item->tax_purchase_price,
                );
                $key = $_maintenance_storage_item->item_type . '-' . $_maintenance_storage_item->item_id;
                if(isset($storage_item[$key])){
                    $storage_item[$key]['number'] = $storage_item[$key]['number'] + $_storage_item['number'];
                    $storage_item[$key]['tax_purchase_subtotal'] = $storage_item[$key]['tax_purchase_subtotal'] + $_storage_item['tax_purchase_subtotal'];
                }else{
                    $storage_item[$key] = $_storage_item;
                }
            }
        }
        return $storage_item;
    }

    /*服务订单维修出库*/
    public static function get_service_order_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $order_out_item = array();
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop)
            ->where('order_type',1)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            //查找领料 或者退货的  因为 存在结算后退货的情况
            $maintenance_out_order = MaintenanceOutOrder::where('order_id',$_order->order_id)
                ->whereIn('status',array(2,3))
                ->get();
            foreach ($maintenance_out_order as $_maintenance_out_order){
                //结算后退货的数量
                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->sum("number");
                if($before_return==0&&$_maintenance_out_order->status==3){//如果是 这个出库申请是结算前 退货完成的  不统计
                    continue;
                }
                //查看 该出库单 结算后退库 多少
                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->get();
                $before_return_array=array();//出库批次为键 退了多少为值的 数组
                foreach ($before_return as $_br) {
                    $temp_msi=MaintenanceStorageItem::where("return_order_id",$_br->id)->where("status",1)->get();
                    foreach ($temp_msi as $_temp_msi) {
                        // $before_return_array[$temp_msi->out_id]=$temp_msi->number;
                        if(isset($before_return_array[$_temp_msi->out_id])){
                            $before_return_array[$_temp_msi->out_id]=$before_return_array[$_temp_msi->out_id]+$_temp_msi->number;
                        }else{
                            $before_return_array[$_temp_msi->out_id]=$_temp_msi->number;
                        }
                    }
                }


                $maintenance_out_item = MaintenanceOutItem::where('order_out_id',$_maintenance_out_order->id)
                    // ->where('data_id',$data_id)
                    ->where('status',1)
                    ->get();
                foreach ($maintenance_out_item as $_maintenance_out_item){
                    $e_number=isset($before_return_array[$_maintenance_out_item->id])?$before_return_array[$_maintenance_out_item->id]:0;//该批次的结算后退货 多少个
                    $_order_out_item = array(
                        'code' => $_maintenance_out_item->code,
                        'name' => $_maintenance_out_item->name,
                        'data_id' => $_maintenance_out_item->data_id,
                        'item_id' =>  $_maintenance_out_item->item_id,
                        'item_type' =>  $_maintenance_out_item->item_type,
                        'number' => $_maintenance_out_item->number - $_maintenance_out_item->return_numer+$e_number,
                        'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price,
                        'tax_purchase_subtotal' => ($_maintenance_out_item->number - $_maintenance_out_item->return_numer+$e_number) * $_maintenance_out_item->tax_purchase_price,
                    );
                    $key = $_maintenance_out_item->item_type . '-' . $_maintenance_out_item->item_id;
                    if(isset($order_out_item[$key])){
                        $order_out_item[$key]['number'] = $order_out_item[$key]['number'] + $_order_out_item['number'];
                        $order_out_item[$key]['tax_purchase_subtotal'] = $order_out_item[$key]['tax_purchase_subtotal'] + $_order_out_item['tax_purchase_subtotal'];
                    }else{
                        $order_out_item[$key] = $_order_out_item;
                    }
                }
            }
        }
        return $order_out_item;
    }

    /*零售订单销售出库*/
    public static function get_sale_order_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $order_out_item = array();
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop)
            ->where('order_type',2)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            $maintenance_out_order = MaintenanceOutOrder::where('order_id',$_order->order_id)
                ->whereIn('status',array(2,3))
                ->get();
            foreach ($maintenance_out_order as $_maintenance_out_order){
                //结算后退货的数量
                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->sum("number");
                if($before_return==0&&$_maintenance_out_order->status==3){//如果是 这个出库申请是结算前 退货完成的  不统计
                    continue;
                }
                //查看 该出库单 结算后退库 多少
                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->get();
                $before_return_array=array();//出库批次为键 退了多少为值的 数组
                foreach ($before_return as $_br) {
                    $temp_msi=MaintenanceStorageItem::where("return_order_id",$_br->id)->where("status",1)->get();
                    foreach ($temp_msi as $_temp_msi) {
                        if(isset($before_return_array[$_temp_msi->out_id])){
                            $before_return_array[$_temp_msi->out_id]=$before_return_array[$_temp_msi->out_id]+$_temp_msi->number;
                        }else{
                            $before_return_array[$_temp_msi->out_id]=$_temp_msi->number;
                        }
                    }
                }
                $maintenance_out_item = MaintenanceOutItem::where('order_out_id',$_maintenance_out_order->id)
                    // ->where('data_id',$data_id)
                    ->where('status',1)
                    ->get();

                foreach ($maintenance_out_item as $_maintenance_out_item){
                    $e_number=isset($before_return_array[$_maintenance_out_item->id])?$before_return_array[$_maintenance_out_item->id]:0;//该批次的结算后退货 多少个
                    $_order_out_item = array(
                        'code' => $_maintenance_out_item->code,
                        'name' => $_maintenance_out_item->name,
                        'data_id' => $_maintenance_out_item->data_id,
                        'item_id' =>  $_maintenance_out_item->item_id,
                        'item_type' =>  $_maintenance_out_item->item_type,
                        'number' => $_maintenance_out_item->number - $_maintenance_out_item->return_numer+$e_number,
                        'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price,
                        'tax_purchase_subtotal' => ($_maintenance_out_item->number - $_maintenance_out_item->return_numer+$e_number) * $_maintenance_out_item->tax_purchase_price,
                    );
                    $key = $_maintenance_out_item->item_type . '-' . $_maintenance_out_item->item_id;
                    if(isset($order_out_item[$key])){
                        $order_out_item[$key]['number'] = $order_out_item[$key]['number'] + $_order_out_item['number'];
                        $order_out_item[$key]['tax_purchase_subtotal'] = $order_out_item[$key]['tax_purchase_subtotal'] + $_order_out_item['tax_purchase_subtotal'];
                    }else{
                        $order_out_item[$key] = $_order_out_item;
                    }
                }
            }
        }
        return $order_out_item;
    }

    /*供货商退货出库*/
    public static function get_supplier_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $supplier_out_item = array();
        $maintenance_out_list = MaintenanceOutList::where('shop_id',$shop)
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->where('type',2)
            ->where('status',1)
            ->get();
        foreach ($maintenance_out_list as $_maintenance_out_list){
            $maintenance_out_item = MaintenanceOutItem::where('list_id',$_maintenance_out_list->id)
                // ->where('data_id',$data_id)
                ->where('status',1)
                ->get();
            foreach ($maintenance_out_item as $_maintenance_out_item){
                $_supplier_out_item = array(
                    'code' => $_maintenance_out_item->code,
                    'name' => $_maintenance_out_item->name,
                    'data_id' => $_maintenance_out_item->data_id,
                    'item_id' =>  $_maintenance_out_item->item_id,
                    'item_type' =>  $_maintenance_out_item->item_type,
                    'number' => $_maintenance_out_item->number,
                    'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price,
                    'tax_purchase_subtotal' => $_maintenance_out_item->number * $_maintenance_out_item->tax_purchase_price,
                );
                $key = $_maintenance_out_item->item_type . '-' . $_maintenance_out_item->item_id;
                if(isset($supplier_out_item[$key])){
                    $supplier_out_item[$key]['number'] = $supplier_out_item[$key]['number'] + $_supplier_out_item['number'];
                    $supplier_out_item[$key]['tax_purchase_subtotal'] = $supplier_out_item[$key]['tax_purchase_subtotal'] + $_supplier_out_item['tax_purchase_subtotal'];
                }else{
                    $supplier_out_item[$key] = $_supplier_out_item;
                }
            }
        }
        return $supplier_out_item;
    }

    /*门店间内部调出出库*/
    public static function get_transfer_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_list = MaintenanceOutList::where('shop_id',$shop)
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->where('type',1)
            ->where('status',1)
            ->get();
        foreach ($maintenance_out_list as $_maintenance_out_list){
            $maintenance_out_item = MaintenanceOutItem::where('list_id',$_maintenance_out_list->id)
                // ->where('data_id',$data_id)
                ->where('status',1)
                ->get();
            foreach ($maintenance_out_item as $_maintenance_out_item){
                $_transfer_out_item = array(
                    'code' => $_maintenance_out_item->code,
                    'name' => $_maintenance_out_item->name,
                    'data_id' => $_maintenance_out_item->data_id,
                    'item_id' =>  $_maintenance_out_item->item_id,
                    'item_type' =>  $_maintenance_out_item->item_type,
                    'number' => $_maintenance_out_item->number,
                    'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price,
                    'tax_purchase_subtotal' => $_maintenance_out_item->number * $_maintenance_out_item->tax_purchase_price,
                );
                $key = $_maintenance_out_item->item_type . '-' . $_maintenance_out_item->item_id;
                if(isset($transfer_out_item[$key])){
                    $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_transfer_out_item['number'];
                    $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_transfer_out_item['tax_purchase_subtotal'];
                }else{
                    $transfer_out_item[$key] = $_transfer_out_item;
                }
            }
        }
        return $transfer_out_item;
    }


    /*门店间内部调出出库*/
    //领用的
    public static function get_transfer_out_data_for_jxc_ly($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_list = MaintenanceOutList::where('shop_id',$shop)
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->where('type',1)
            ->where('methed',1)//领用
            ->where('status',1)
            ->get();
        foreach ($maintenance_out_list as $_maintenance_out_list){
            $maintenance_out_item = MaintenanceOutItem::where('list_id',$_maintenance_out_list->id)
                // ->where('data_id',$data_id)
                ->where('status',1)
                ->get();
            foreach ($maintenance_out_item as $_maintenance_out_item){
                $_transfer_out_item = array(
                    'code' => $_maintenance_out_item->code,
                    'name' => $_maintenance_out_item->name,
                    'data_id' => $_maintenance_out_item->data_id,
                    'item_id' =>  $_maintenance_out_item->item_id,
                    'item_type' =>  $_maintenance_out_item->item_type,
                    'number' => $_maintenance_out_item->number,
                    'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price,
                    'tax_purchase_subtotal' => $_maintenance_out_item->number * $_maintenance_out_item->tax_purchase_price,
                );
                $key = $_maintenance_out_item->item_type . '-' . $_maintenance_out_item->item_id;
                if(isset($transfer_out_item[$key])){
                    $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_transfer_out_item['number'];
                    $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_transfer_out_item['tax_purchase_subtotal'];
                }else{
                    $transfer_out_item[$key] = $_transfer_out_item;
                }
            }
        }
        return $transfer_out_item;
    }

    /*门店间内部调出出库*/
    //调出
    public static function get_transfer_out_data_for_jxc_dc($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_list = MaintenanceOutList::where('shop_id',$shop)
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->where('type',1)
            ->where('methed',2)//调出
            ->where('status',1)
            ->get();
        foreach ($maintenance_out_list as $_maintenance_out_list){
            $maintenance_out_item = MaintenanceOutItem::where('list_id',$_maintenance_out_list->id)
                // ->where('data_id',$data_id)
                ->where('status',1)
                ->get();
            foreach ($maintenance_out_item as $_maintenance_out_item){
                $_transfer_out_item = array(
                    'code' => $_maintenance_out_item->code,
                    'name' => $_maintenance_out_item->name,
                    'data_id' => $_maintenance_out_item->data_id,
                    'item_id' =>  $_maintenance_out_item->item_id,
                    'item_type' =>  $_maintenance_out_item->item_type,
                    'number' => $_maintenance_out_item->number,
                    'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price,
                    'tax_purchase_subtotal' => $_maintenance_out_item->number * $_maintenance_out_item->tax_purchase_price,
                );
                $key = $_maintenance_out_item->item_type . '-' . $_maintenance_out_item->item_id;
                if(isset($transfer_out_item[$key])){
                    $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_transfer_out_item['number'];
                    $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_transfer_out_item['tax_purchase_subtotal'];
                }else{
                    $transfer_out_item[$key] = $_transfer_out_item;
                }
            }
        }
        return $transfer_out_item;
    }


    /*门店间内部调出出库*/
    //领用的
    public static function get_transfer_out_data_for_jxc_xs($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_list = MaintenanceOutList::where('shop_id',$shop)
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->where('type',1)
            ->where('methed',3)//销售
            ->where('status',1)
            ->get();
        foreach ($maintenance_out_list as $_maintenance_out_list){
            $maintenance_out_item = MaintenanceOutItem::where('list_id',$_maintenance_out_list->id)
                // ->where('data_id',$data_id)
                ->where('status',1)
                ->get();
            foreach ($maintenance_out_item as $_maintenance_out_item){
                $_transfer_out_item = array(
                    'code' => $_maintenance_out_item->code,
                    'name' => $_maintenance_out_item->name,
                    'data_id' => $_maintenance_out_item->data_id,
                    'item_id' =>  $_maintenance_out_item->item_id,
                    'item_type' =>  $_maintenance_out_item->item_type,
                    'number' => $_maintenance_out_item->number,
                    'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price,
                    'tax_purchase_subtotal' => $_maintenance_out_item->number * $_maintenance_out_item->tax_purchase_price,
                );
                $key = $_maintenance_out_item->item_type . '-' . $_maintenance_out_item->item_id;
                if(isset($transfer_out_item[$key])){
                    $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_transfer_out_item['number'];
                    $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_transfer_out_item['tax_purchase_subtotal'];
                }else{
                    $transfer_out_item[$key] = $_transfer_out_item;
                }
            }
        }
        return $transfer_out_item;
    }



    /*盘点盘盈调整入库*/
    public static function get_inventory_storage_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $inventory_storage_item = array();
        $maintenance_inventory_storage = MaintenanceInventoryStorage::where('shop_id',$shop)
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->where('status',1)
            ->where('number','>',0)
            ->get();
        foreach ($maintenance_inventory_storage as $_maintenance_inventory_storage){
            // $maintenance_storage_item = MaintenanceStorageItem::where('id',$_maintenance_inventory_storage->storage_id)->where('data_id',$data_id)->first();
            $maintenance_storage_item = MaintenanceStorageItem::where('id',$_maintenance_inventory_storage->storage_id)->first();
            if(is_null($maintenance_storage_item)) continue;
            $_inventory_storage_item = array(
                'code' => $maintenance_storage_item->code,
                'name' => $maintenance_storage_item->name,
                'data_id' => $maintenance_storage_item->data_id,
                'item_id' =>  $maintenance_storage_item->item_id,
                'item_type' =>  $maintenance_storage_item->item_type,
                'number' => abs($_maintenance_inventory_storage->number),
                'tax_purchase_price' => $maintenance_storage_item->tax_purchase_price,
                'tax_purchase_subtotal' => abs($_maintenance_inventory_storage->number) * $maintenance_storage_item->tax_purchase_price,
            );
            $key = $maintenance_storage_item->item_type . '-' . $maintenance_storage_item->item_id;
            if(isset($inventory_storage_item[$key])){
                $inventory_storage_item[$key]['number'] = $inventory_storage_item[$key]['number'] + $_inventory_storage_item['number'];
                $inventory_storage_item[$key]['tax_purchase_subtotal'] = $inventory_storage_item[$key]['tax_purchase_subtotal'] + $_inventory_storage_item['tax_purchase_subtotal'];
            }else{
                $inventory_storage_item[$key] = $_inventory_storage_item;
            }
        }
        return $inventory_storage_item;
    }
    /*盘点盘亏调整出库*/
    public static function get_inventory_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $inventory_out_item = array();
        $maintenance_inventory_storage = MaintenanceInventoryStorage::where('shop_id',$shop)
            ->whereBetween('implement_time',array($begin_date,$end_date))
            ->where('status',1)
            ->where('number','<',0)
            ->get();
        foreach ($maintenance_inventory_storage as $_maintenance_inventory_storage){
            // $maintenance_storage_item = MaintenanceStorageItem::where('id',$_maintenance_inventory_storage->storage_id)->where('data_id',$data_id)->first();
            $maintenance_storage_item = MaintenanceStorageItem::where('id',$_maintenance_inventory_storage->storage_id)->first();
            if(is_null($maintenance_storage_item)) continue;
            $_inventory_out_item = array(
                'code' => $maintenance_storage_item->code,
                'name' => $maintenance_storage_item->name,
                'data_id' => $maintenance_storage_item->data_id,
                'item_id' =>  $maintenance_storage_item->item_id,
                'item_type' =>  $maintenance_storage_item->item_type,
                'number' => abs($_maintenance_inventory_storage->number),
                'tax_purchase_price' => $maintenance_storage_item->tax_purchase_price,
                'tax_purchase_subtotal' => abs($_maintenance_inventory_storage->number) * $maintenance_storage_item->tax_purchase_price,
            );
            $key = $maintenance_storage_item->item_type . '-' . $maintenance_storage_item->item_id;
            if(isset($inventory_out_item[$key])){
                $inventory_out_item[$key]['number'] = $inventory_out_item[$key]['number'] + $_inventory_out_item['number'];
                $inventory_out_item[$key]['tax_purchase_subtotal'] = $inventory_out_item[$key]['tax_purchase_subtotal'] + $_inventory_out_item['tax_purchase_subtotal'];
            }else{
                $inventory_out_item[$key] = $_inventory_out_item;
            }
        }
        return $inventory_out_item;
    }

    /*入库商品明细*/
    public static function get_maintenance_storage_detail($shop_id,$begin_date,$end_date){
        $storage_detail = array();
        //获取入库单
        $storage_list = MaintenanceStorageList::whereBetween('date',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('status',1)
            ->get();
        foreach ($storage_list as $_storage_list){
            $storage_item = MaintenanceStorageItem::where('list_id',$_storage_list->id)
                ->where('status',1)
                ->get();

            $supplier = MaintenanceSupplier::where('id',$_storage_list->supplier)->first();

            if($_storage_list->storage_type == 0) $storage_type = '购入';
            elseif ($_storage_list->storage_type == 1) $storage_type = '调入';
            elseif ($_storage_list->storage_type == 2) $storage_type = '订单退货';
            elseif ($_storage_list->storage_type == 3) $storage_type = '内部领用';
            else $storage_type = $_storage_list->storage_type;

            if($_storage_list->methed == 0) $methed = '领用';
            elseif ($_storage_list->methed == 1) $methed = '调出';
            else $methed = $_storage_list->methed;

            if($_storage_list->pay_type == 0) $pay_type = '现金';
            elseif ($_storage_list->pay_type == 1) $pay_type = '刷卡';
            elseif ($_storage_list->pay_type == 2) $pay_type = '挂账';
            else $pay_type = $_storage_list->pay_type;

            if($_storage_list->type == 1) $type = '批量临时';
            elseif ($_storage_list->type == 2) $type = '领用归还';
            elseif ($_storage_list->type == 3) $type = '订单退货';
            elseif ($_storage_list->type == 3) $type = '零售退货';
            else $type = $_storage_list->type;

            foreach ($storage_item as $_storage_item){
                $item_detail = Report::get_supply_detail($_storage_item->item_id,$_storage_item->item_type);
                $part_classification = Report::get_part_classification($item_detail->st_classifi_id);

                if($_storage_item->type == 1) $item_type = '入库';
                elseif ($_storage_item->type == 2) $item_type = '退库';
                else $item_type = $_storage_item->type;

                $_storage_detail = array(
                    '入库单ID' => $_storage_list->id,
                    '门店' => $_storage_list->shop_id,
                    '入库时间' => $_storage_list->date,
                    '供货商' => $supplier->name,
                    '入库方式' => $storage_type,
                    '领用门店ID' => $_storage_list->consuming_id,
                    '领用方式' => $methed,
                    '结算方式' => $pay_type,
                    '类型' => $type,
                    '备注' => $_storage_list->desc,
                    '商品编码' => $_storage_item->code,
                    '商品名称' => $_storage_item->name,
                    '含税进价' => $_storage_item->tax_purchase_price,
                    '无税进价' => $_storage_item->no_tax_purchase_price,
                    '含税小计' => $_storage_item->tax_purchase_subtotal,
                    '无税计划成本' => $_storage_item->no_tax_plan_costs,
                    '含税计划成本' => $_storage_item->tax_plan_costs,
                    '入库数量' => $_storage_item->number,
                    '当前数量' => $_storage_item->current_number,
                    '品类' => $_storage_item->subject,
                    '货位' => $_storage_item->location,
                    '单项类型' => $item_type,
                    '包装' => $_storage_item->pack_unit,
                    '型号' => $_storage_item->model,
                    '创建时间' => $_storage_item->created_at,
                    '更新时间' => $_storage_item->created_at,
                    '一级分类' => $part_classification['level_1_name'],
                    '二级分类' => $part_classification['level_2_name'],
                    '三级分类' => $part_classification['level_3_name'],
                );
                array_push($storage_detail,$_storage_detail);
            }
        }
        return $storage_detail;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-06-03 11:25:27
     * @Description: Description
     */
    public static  function get_supply_stock($begin_date,$end_date,$date_id,$shop_id){
        $finally=array();

        $mw=MaintenanceWarehouse::where("shop_id",$shop_id)->where('created_at','<=',$end_date)->get();//当门店 入库过的耗材
        $begin_date=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));
        //获取 该库的期初库存
        $start_stock_array=array();
        $start_stock=MaintenanceWarehouseStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($begin_date)))->get();
        foreach ($start_stock as $_start_stock) {
            if(isset($start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id])){
                $temp=$start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id];
                $temp->number=$temp->number+$_start_stock->number;
                $temp->amount=$temp->amount+$_start_stock->amount;
                $start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id]=$temp;
            }else{
                $start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id]=$_start_stock;
            }
        }

        //获取该库的期末库存
        $end_stock_array=array();
        $end_stock=MaintenanceWarehouseStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($end_date)))->get();
        foreach ($end_stock as $_end_stock) {
            if(isset($end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id])){
                $temp=$end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id];
                $temp->number=$temp->number+$_end_stock->number;
                $temp->amount=$temp->amount+$_end_stock->amount;
                $end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id]=$temp;
            }else{
                $end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id]=$_end_stock;
            }
        }

        //获取 该库的 这段时间的 入库成本
        $into_stock_array=array();
        $sql="select item_type,item_id,sum(number) number,sum(number*tax_purchase_price) amount from maintenance_storage_item msi
            where msi.shop_id=? and msi.status=1 and msi.created_at>? and  msi.created_at<=?  group by msi.item_type,msi.item_id";
        $sin_item=DB::select($sql,array($shop_id,$begin_date,$end_date));
        foreach ($sin_item as $_sin_item) {
            $into_stock_array[$_sin_item->item_type.'-'.$_sin_item->item_id]=$_sin_item;
        }
        $tuigong_sql="SELECT  item_type,item_id,- sum(number) number,- sum(number*tax_purchase_price) amount FROM maintenance_out_item moi 
                LEFT JOIN maintenance_out_list mol  on moi.list_id=mol.id WHERE    moi.shop_id =? and mol.`status`=1 and mol.type=2  AND moi. STATUS = 1 AND moi.created_at >? AND moi.created_at <=?  GROUP BY  moi.item_type,moi.item_id";
        $tuigong_item=DB::select($tuigong_sql,array($shop_id,$begin_date,$end_date));
        foreach ($tuigong_item as $_tuigong_item) {
            if(isset($into_stock_array[$_tuigong_item->item_type.'-'.$_tuigong_item->item_id])){
                $temp_object=$into_stock_array[$_tuigong_item->item_type.'-'.$_tuigong_item->item_id];
                $temp_object->number=$temp_object->number+$_tuigong_item->number;
                $temp_object->amount=$temp_object->amount+$_tuigong_item->amount;
                $into_stock_array[$_tuigong_item->item_type.'-'.$_tuigong_item->item_id]=$temp_object;
            }else{
                $into_stock_array[$_tuigong_item->item_type.'-'.$_tuigong_item->item_id]=$_tuigong_item;
            }
        }

        /**
         *去掉没有出库申请的 订单退库入库数量 暂时使用的查询
         */
        $tui_tichu_sql="SELECT sum(msi.number) number,msi.item_id,msi.item_type 
            from maintenance_storage_item msi 
            left join maintenance_return_order mro  on msi.return_order_id=mro.id 
            LEFT JOIN maintenance_out_order moo on mro.out_order_id=moo.id 
            where msi.shop_id =? AND msi.created_at >? AND msi.created_at <=? AND msi.`status`=1 and moo.id is null and mro.id is not null GROUP BY msi.item_id,msi.item_type ";
        $tui_tichu_item=DB::select($tui_tichu_sql,array($shop_id,$begin_date,$end_date));
        foreach ($tui_tichu_item as $_tui_tichu_item) {
            if(isset($into_stock_array[$_tui_tichu_item->item_type.'-'.$_tui_tichu_item->item_id])){
                $temp_object=$into_stock_array[$_tui_tichu_item->item_type.'-'.$_tui_tichu_item->item_id];
                $temp_object->number=$temp_object->number-$_tui_tichu_item->number;
                $into_stock_array[$_tui_tichu_item->item_type.'-'.$_tui_tichu_item->item_id]=$temp_object;
            }
        }

        //获取该段时间的 维修出库的成本
        $orderout_stock_array=array();
        $order_sql="SELECT  moi.item_type,moi.item_id,sum(moi.number) number,sum(moi.number * moi.tax_purchase_price) amount FROM     maintenance_out_item moi 
                left join maintenance_out_order moo on moo.id=moi.order_out_id 
                LEFT JOIN orders o on o.order_id=moo.order_id 
                LEFT JOIN maintenance_out_list mol ON moi.list_id = mol.id 
                WHERE  moi.shop_id =? AND mol.`status` = 1 AND mol.type = 3 AND moi. STATUS = 1 AND moi.created_at >? AND moi.created_at <=? and o.place=2 and o.order_type=1 GROUP BY  moi.item_type,  moi.item_id";
        $sorder_item=DB::select($order_sql,array($shop_id,$begin_date,$end_date));
        foreach ($sorder_item as $_sorder_item) {
            $orderout_stock_array[$_sorder_item->item_type.'-'.$_sorder_item->item_id]=$_sorder_item;
        }
        $order_tui_sql="SELECT moi.item_id,moi.item_type,0 number,- sum( if(mro.number>=moi.return_numer,moi.return_numer* moi.tax_purchase_price,mro.number * moi.tax_purchase_price)) amount from maintenance_return_order mro 
                LEFT JOIN orders o on o.order_id=mro.order_id 
                LEFT JOIN maintenance_out_order moo on mro.out_order_id=moo.id  
                LEFT JOIN maintenance_out_item moi on moi.order_out_id=moo.id 
                where mro.status=1 and mro.shop_id =? AND mro.created_at >? AND mro.created_at <=? and o.place=2 and o.order_type=1 group by moi.item_type,moi.item_id";
        $order_tui_item=DB::select($order_tui_sql,array($shop_id,$begin_date,$end_date));
        foreach ($order_tui_item as $_order_tui_item) {
            if(isset($orderout_stock_array[$_order_tui_item->item_type.'-'.$_order_tui_item->item_id])){
                $temp_object=$orderout_stock_array[$_order_tui_item->item_type.'-'.$_order_tui_item->item_id];
                $temp_object->amount=$temp_object->amount+$_order_tui_item->amount;
                $orderout_stock_array[$_order_tui_item->item_type.'-'.$_order_tui_item->item_id]=$temp_object;
            }else{
                $orderout_stock_array[$_order_tui_item->item_type.'-'.$_order_tui_item->item_id]=$_order_tui_item;
            }
        }

        // 获取该段时间内 零售出库的成本
        $saleout_stock_array=array();
        $sqle_sql="SELECT  moi.item_type,moi.item_id,sum(moi.number) number,sum((moi.number) * moi.tax_purchase_price) amount FROM     maintenance_out_item moi 
                left join maintenance_out_order moo on moo.id=moi.order_out_id 
                LEFT JOIN orders o on o.order_id=moo.order_id 
                LEFT JOIN maintenance_out_list mol ON moi.list_id = mol.id 
                WHERE  moi.shop_id =? AND mol.`status` = 1 AND mol.type = 3 AND moi. STATUS = 1 AND moi.created_at >? AND moi.created_at <=? and o.place=2 and o.order_type=2 GROUP BY  moi.item_type,  moi.item_id";
        $sale_item=DB::select($sqle_sql,array($shop_id,$begin_date,$end_date));
        foreach ($sale_item as $_sale_item) {
            $saleout_stock_array[$_sale_item->item_type.'-'.$_sale_item->item_id]=$_sale_item;
        }

        $sale_tui_sql="SELECT moi.item_id,moi.item_type,0 number,- sum(if(mro.number>=moi.return_numer,moi.return_numer* moi.tax_purchase_price,mro.number * moi.tax_purchase_price)) amount from maintenance_return_order mro 
                LEFT JOIN orders o on o.order_id=mro.order_id 
                LEFT JOIN maintenance_out_order moo on mro.out_order_id=moo.id  
                LEFT JOIN maintenance_out_item moi on moi.order_out_id=moo.id 
                where mro.status=1 and mro.shop_id =? AND mro.created_at >? AND mro.created_at <=? and o.place=2 and o.order_type=2 group by moi.item_type,moi.item_id";
        $sale_tui_item=DB::select($sale_tui_sql,array($shop_id,$begin_date,$end_date));
        foreach ($sale_tui_item as $_sale_tui_item) {
            if(isset($saleout_stock_array[$_sale_tui_item->item_type.'-'.$_sale_tui_item->item_id])){
                $temp_object=$saleout_stock_array[$_sale_tui_item->item_type.'-'.$_sale_tui_item->item_id];
                $temp_object->amount=$temp_object->amount+$_sale_tui_item->amount;
                $saleout_stock_array[$_sale_tui_item->item_type.'-'.$_sale_tui_item->item_id]=$temp_object;
            }else{
                $saleout_stock_array[$_sale_tui_item->item_type.'-'.$_sale_tui_item->item_id]=$_sale_tui_item;
            }
        }

        //该段时间调出成本
        $diaochu_stock_array=array();
        $order_sql="SELECT  item_type,item_id,sum(number) number,sum(number*tax_purchase_price) amount FROM maintenance_out_item moi 
                LEFT JOIN maintenance_out_list mol  on moi.list_id=mol.id WHERE    moi.shop_id =? and mol.`status`=1 and mol.type=1  AND moi. STATUS = 1 AND moi.created_at >? AND moi.created_at <=?  GROUP BY  moi.item_type,moi.item_id";
        $diaochu_item=DB::select($order_sql,array($shop_id,$begin_date,$end_date));
        foreach ($diaochu_item as $_diaochu_item) {
            $diaochu_stock_array[$_diaochu_item->item_type.'-'.$_diaochu_item->item_id]=$_diaochu_item;
        }

        $diaochu_tui_sql="SELECT msi.item_id,msi.item_type ,0 number,sum(msi.number*moi.tax_purchase_price) amount
            FROM
                maintenance_storage_item msi
            LEFT JOIN maintenance_storage_list msl ON msi.list_id=msl.id 
            left join maintenance_out_item moi on moi.id=msi.out_id
            WHERE msi.shop_id =?
            AND msi.created_at >? AND msi.created_at <=?
            AND msl.`status` = 1
            AND msi.`status` = 1
            AND msl.type = 2
            group by msi.item_type,msi.item_id";
        $daoo_chutui_item=DB::select($diaochu_tui_sql,array($shop_id,$begin_date,$end_date));
        foreach ($daoo_chutui_item as $_daoo_chutui_item) {
            if(isset($diaochu_stock_array[$_daoo_chutui_item->item_type.'-'.$_daoo_chutui_item->item_id])){
                $temp_object=$diaochu_stock_array[$_daoo_chutui_item->item_type.'-'.$_daoo_chutui_item->item_id];
                $temp_object->amount=$temp_object->amount+$_daoo_chutui_item->amount;
                $diaochu_stock_array[$_daoo_chutui_item->item_type.'-'.$_daoo_chutui_item->item_id]=$temp_object;
            }else{
                $diaochu_stock_array[$_daoo_chutui_item->item_type.'-'.$_daoo_chutui_item->item_id]=$_daoo_chutui_item;
            }
        }



        //查看盘点差异
        $pandian_stock_array=array();
        $order_sql="SELECT  mis.item_id,mis.item_type,sum(mis.number) number,sum(mis.number*msi.tax_purchase_price ) amount  FROM   `maintenance_inventory_storage` mis LEFT JOIN maintenance_storage_item msi ON mis.storage_id = msi.id where mis.`status`=1 and mis.created_at >? AND mis.created_at <=? GROUP BY mis.item_type,mis.item_id";
        $pandian_item=DB::select($order_sql,array($date_id,$begin_date,$end_date));
        foreach ($pandian_item as $_pandian_item) {
            $pandian_stock_array[$_pandian_item->item_type.'-'.$_pandian_item->item_id]=$_pandian_item;
        }
        // print_r($end_stock_array);
        // exit;
        foreach ($mw as $_mv) {
            $key=$_mv->item_type.'-'.$_mv->item_id;
            //获取耗材信息
           // if($_mv->item_type==0){
           //     $item_info=SupplyItem::getCacheItemInfo($_mv->item_id);
           // }else{
           //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_mv->item_id);
           // }
           // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
            $item_info = SupplyItem::find($_mv->item_id);
            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
            //获取 时间段内的入库成本
            $temp_array=array(
                'item_type' => $_mv->item_type,
                'item_id' => $_mv->item_id,
                'item_brand_name' => $item_info->brand_name,
                'item_series_name' => $item_info->series_name,
                'item_model' => $item_info->model,
                'item_code' => $item_info->code,
               // 'item_specification' => $item_info->specification,
               // 'item_bz_unit' => $item_info->bz_unit,
                'item_pack' => $item_info->volume_size,
                'item_name' => $item_info->name,
               // 'item_unit' => $item_info->unit,
                'st_classifi_id' => $item_info->supply_classification_id,
                'level_1_id' => $part_classification['level_1_id'],
                'level_1_name' => $part_classification['level_1_name'],
                'level_2_id' => $part_classification['level_2_id'],
                'level_2_name' => $part_classification['level_2_name'],
                'level_3_id' => $part_classification['level_3_id'],
                'level_3_name' => $part_classification['level_3_name'],
                'start_stock' => isset($start_stock_array[$key]->number)?$start_stock_array[$key]->number:0,
                'start_amount' => isset($start_stock_array[$key]->amount)?$start_stock_array[$key]->amount:0,
                'into_stock' => isset($into_stock_array[$key]->number)?$into_stock_array[$key]->number:0,
                'into_amount' => isset($into_stock_array[$key]->amount)?$into_stock_array[$key]->amount:0,
                'orderout_stock' => isset($orderout_stock_array[$key]->number)?$orderout_stock_array[$key]->number:0,
                'orderout_amount' => isset($orderout_stock_array[$key]->amount)?$orderout_stock_array[$key]->amount:0,
                'saleout_stock' => isset($saleout_stock_array[$key]->number)?$saleout_stock_array[$key]->number:0,
                'saleout_amount' => isset($saleout_stock_array[$key]->amount)?$saleout_stock_array[$key]->amount:0,
                'diaochu_stock' => isset($diaochu_stock_array[$key]->number)?$diaochu_stock_array[$key]->number:0,
                'diaochu_amount' => isset($diaochu_stock_array[$key]->amount)?$diaochu_stock_array[$key]->amount:0,
                'pandian_stock' => isset($pandian_stock_array[$key]->number)?$pandian_stock_array[$key]->number:0,
                'pandian_amount' => isset($pandian_stock_array[$key]->amount)?$pandian_stock_array[$key]->amount:0,
                'end_stock' => isset($end_stock_array[$key]->number)?$end_stock_array[$key]->number:0,
                'end_amount' => isset($end_stock_array[$key]->amount)?$end_stock_array[$key]->amount:0,
            );
            $finally[]=$temp_array;
        }
        return $finally;
    }
    //技师工作绩效明细
    public static function get_engineer_work_detail($begin_date,$end_date,$shop_id){
        $order_product_detail = array();
        //获取订单
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('order_type',1)
            ->where('place',2)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach ($order as $_order){
            //获取订单下产品
            $order_product = OrderProduct::where('order_id',$_order->order_id)
                ->where('status',1)
                ->get();
            //获取服务产品包的三级分类
            $order_engineer = MaintenanceEngineers::find($_order->engineer_id);
            foreach ($order_product as $_order_product) {
                if($_order_product->product_type=='enterprise'){//说明是 企业报
                    $opep=OrderProductEsPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $service_detail = Report::get_service_detail($_opep->package_id,'base');
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        if($_opep->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                        }
                        //获取耗材成本
                        $supply_amount=0;
                        $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='enterprise' and product_item_id in(SELECT id FROM `order_product_esitem` where order_product_package_id=".$_opep->id." and type=1)");
                        foreach ($mot_ids as $_mot) {
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                            if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                continue;
                            }
                            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();

                            $temp_amount=0;
                            foreach ($before_return_ids as $bri) {
                                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                $temp_amount+= $return_amount[0]->amount;
                            }


                            $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                            $supply_amount+=($supply_count[0]->amount+$temp_amount);
                        }
                        $_order_product_detail = array(
                            'order_id' => $_order->order_id,
                            'pay_time' => $_order->pay_time,
                            'car_no' => $_order->car_no,
                            'name' => $_order->name,
                            'order_engineer' => $order_engineer->name,
                            'order_engineer_id' => $order_engineer->id,
                            'product_name' => $_order_product->product_name." ".$_opep->package_name ,
                            'product_engineer' => $product_engineer->name,
                            'product_engineer_id' => $product_engineer->id,
                            'product_amount' => $_opep->amount,
                            'product_discount' => 0,
                            'counter_amount' => $_opep->counter_amount,
                            'supply_amount' => $supply_amount,
                            'level_1_id' => $service_classification['level_1_id'],
                            'level_1_name' => $service_classification['level_1_name'],
                            'level_2_id' => $service_classification['level_2_id'],
                            'level_2_name' => $service_classification['level_2_name'],
                            'level_3_id' => $service_classification['level_3_id'],
                            'level_3_name' => $service_classification['level_3_name'],
                            'reteurn_type' => 0,
                        );
                        array_push($order_product_detail,$_order_product_detail);
                    }
                }elseif($_order_product->product_type=='activity'){//说明是 活动包
                    $opep=OrderProductAcPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $service_detail = Report::get_service_detail($_opep->package_id,'base');
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        if($_opep->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                        }
                        $supply_amount=0;
                        //获取标准的工时成本
                        $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_acitem` where order_product_package_id=".$_opep->id." and type=2");
                        $service_amount=$count[0]->amount;
                        //获取耗材成本
                        $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='activity' and product_item_id in(SELECT id FROM `order_product_acitem` where order_product_package_id=".$_opep->id." and type=1)");
                        foreach ($mot_ids as $_mot) {
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                            if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                continue;
                            }
                            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();

                            $temp_amount=0;
                            foreach ($before_return_ids as $bri) {
                                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                $temp_amount+= $return_amount[0]->amount;
                            }


                            $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                            $supply_amount+=($supply_count[0]->amount+$temp_amount);
                        }
                        
                        $_order_product_detail = array(
                            'order_id' => $_order->order_id,
                            'pay_time' => $_order->pay_time,
                            'car_no' => $_order->car_no,
                            'name' => $_order->name,
                            'order_engineer' => $order_engineer->name,
                            'order_engineer_id' => $order_engineer->id,
                            'product_name' => $_order_product->product_name." ".$_opep->package_name ,
                            'product_engineer' => $product_engineer->name,
                            'product_engineer_id' => $product_engineer->id,
                            'product_amount' => $_opep->amount,
                            'product_discount' => 0,
                            'counter_amount' => $_opep->counter_amount,
                            'supply_amount' => $supply_amount,
                            'level_1_id' => $service_classification['level_1_id'],
                            'level_1_name' => $service_classification['level_1_name'],
                            'level_2_id' => $service_classification['level_2_id'],
                            'level_2_name' => $service_classification['level_2_name'],
                            'level_3_id' => $service_classification['level_3_id'],
                            'level_3_name' => $service_classification['level_3_name'],
                            'reteurn_type' => 0,
                        );
                        array_push($order_product_detail,$_order_product_detail);
                    }
                }else{
                    $service_detail = Report::get_service_detail($_order_product->product_id, $_order_product->product_type);
                    $service_classification = Report::get_service_classification($service_detail->classify);
                    if($_order_product->engineer_id == 0){
                        $product_engineer = $order_engineer;
                    }else{
                        $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                    }
                    
                        $supply_amount=0;
                        if($_order_product->product_type == 'base'){
                            //获取耗材成本
                            $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='base' and product_item_id in(SELECT id FROM `order_product_base_item` where order_product_package_id=".$_order_product->id." and type=1)");
                            foreach ($mot_ids as $_mot) {
                                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                                if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                    continue;
                                }
                                $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                                $temp_amount=0;
                                foreach ($before_return_ids as $bri) {
                                    $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                    $temp_amount+= $return_amount[0]->amount;
                                }


                                $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                                $supply_amount+=($supply_count[0]->amount+$temp_amount);
                            }
                        }elseif($_order_product->product_type == 'non_standard'){
                            //获取耗材成本
                            $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='non_standard' and product_item_id in(SELECT id FROM `order_product_non_standard` where order_product_id=".$_order_product->id." and service_type=1)");
                            foreach ($mot_ids as $_mot) {
                                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                                if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                    continue;
                                }
                                $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                                $temp_amount=0;
                                foreach ($before_return_ids as $bri) {
                                    $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                    $temp_amount+= $return_amount[0]->amount;
                                }

                                $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                                $supply_amount+=($supply_count[0]->amount+$temp_amount);
                            }
                        }elseif($_order_product->product_type == 'service_card'){
                            //获取耗材成本
                            $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='service_card' and product_item_id in(SELECT id FROM `order_product_sc_item` where order_product_id=".$_order_product->id." and type=1)");
                            foreach ($mot_ids as $_mot) {
                                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                                if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                    continue;
                                }
                                $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                                $temp_amount=0;
                                foreach ($before_return_ids as $bri) {
                                    $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                    $temp_amount+= $return_amount[0]->amount;
                                }


                                $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                                $supply_amount+=($supply_count[0]->amount+$temp_amount);
                            }
                        }

                    $_order_product_detail = array(
                        'order_id' => $_order->order_id,
                        'pay_time' => $_order->pay_time,
                        'car_no' => $_order->car_no,
                        'name' => $_order->name,
                        'order_engineer' => $order_engineer->name,
                        'order_engineer_id' => $order_engineer->id,
                        'product_name' => $_order_product->product_name,
                        'product_engineer' => $product_engineer->name,
                        'product_engineer_id' => $product_engineer->id,
                        'product_amount' => $_order_product->amount,
                        'product_discount' => $_order_product->discount,
                        'counter_amount' => $_order_product->counter_amount,
                        'supply_amount' => $supply_amount,
                        'level_1_id' => $service_classification['level_1_id'],
                        'level_1_name' => $service_classification['level_1_name'],
                        'level_2_id' => $service_classification['level_2_id'],
                        'level_2_name' => $service_classification['level_2_name'],
                        'level_3_id' => $service_classification['level_3_id'],
                        'level_3_name' => $service_classification['level_3_name'],
                        'reteurn_type' => 0,
                    );
                    array_push($order_product_detail,$_order_product_detail);
                }

            }
        }

        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->where('orders.order_type',1)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id',"orders.*"))->orderBy('return_order.implement_time',"asc")->get();

        foreach ($ro as $_ro){
            $order_engineer = MaintenanceEngineers::find($_ro->engineer_id);
            $roi=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->where("type",3)->first();//说明是 均摊的退货
            if($roi){
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",3)->where("type",3)->get();
            }else{
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->get();//
            }
            $pacek_arr=array();
            foreach ($ori as $_roi) {
                if($_roi->type==4){
                    if($_roi->product_type=='enterprise'){//说明是 企业报
                        $_order_product = OrderProduct::find($_roi->order_product_id);
                        $opep=OrderProductEsPackage::where("order_product_id",$_roi->order_product_id)->get();
                        foreach ($opep as $_opep){
                            $service_detail = Report::get_service_detail($_opep->package_id,'base');
                            $service_classification = Report::get_service_classification($service_detail->classify);
                            if($_opep->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                            }
                            $item = OrderProductEsItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
                            foreach($item as $_it){
                                $item_type = $_it->type;
                                $supply_amount=0;                                
                                if($item_type == 1) {
                                    $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                    foreach ($before_return as $_br) {
                                        $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                        foreach ($temp_msi as $_ts) {
                                            $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                            $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                        }
                                    }
                                    $supply_amount = -$supply_amount;
                                }
                            }
                            $_order_product_detail = array(
                                'order_id' => $_ro->order_id,
                                'pay_time' => $_ro->pay_time,
                                'car_no' => $_ro->car_no,
                                'name' => $_ro->name,
                                'order_engineer' => $order_engineer->name,
                                'order_engineer_id' => $order_engineer->id,
                                'product_name' => $_order_product->product_name." ".$_opep->package_name ,
                                'product_engineer' => $product_engineer->name,
                                'product_engineer_id' => $product_engineer->id,
                                'product_amount' => 0,
                                'product_discount' => 0,
                                'counter_amount' => -$_opep->counter_amount,
                                'counter_amount' => $counter_amount,
                                'supply_amount' => $supply_amount,
                                'level_1_id' => $service_classification['level_1_id'],
                                'level_1_name' => $service_classification['level_1_name'],
                                'level_2_id' => $service_classification['level_2_id'],
                                'level_2_name' => $service_classification['level_2_name'],
                                'level_3_id' => $service_classification['level_3_id'],
                                'level_3_name' => $service_classification['level_3_name'],
                                'reteurn_type' => 1,
                            );
                            array_push($order_product_detail,$_order_product_detail);
                        }
                    }elseif($_roi->product_type=='activity'){//说明是 活动包
                        $_order_product = OrderProduct::find($_roi->order_product_id);
                        $opep=OrderProductAcPackage::where("order_product_id",$_order_product->order_product_id)->get();
                        foreach ($opep as $_opep){
                            $service_detail = Report::get_service_detail($_opep->package_id,'base');
                            $service_classification = Report::get_service_classification($service_detail->classify);
                            if($_opep->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::find($_opep->engineer_id);
                            }
                            $item = OrderProductAcItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
                            foreach($item as $_it){
                                $item_type = $_it->type;
                                $supply_amount=0;                                
                                if($item_type == 1) {
                                    $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                    foreach ($before_return as $_br) {
                                        $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                        foreach ($temp_msi as $_ts) {
                                            $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                            $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                        }
                                    }
                                    $supply_amount = -$supply_amount;
                                }
                            }
                            $_order_product_detail = array(
                                'order_id' => $_ro->order_id,
                                'pay_time' => $_ro->pay_time,
                                'car_no' => $_ro->car_no,
                                'name' => $_ro->name,
                                'order_engineer' => $order_engineer->name,
                                'order_engineer_id' => $order_engineer->id,
                                'product_name' => $_order_product->product_name." ".$_opep->package_name ,
                                'product_engineer' => $product_engineer->name,
                                'product_engineer_id' => $product_engineer->id,
                                'product_amount' => 0,
                                'product_discount' => 0,
                                'counter_amount' => -$_opep->counter_amount,
                                'supply_amount' => $supply_amount,
                                'level_1_id' => $service_classification['level_1_id'],
                                'level_1_name' => $service_classification['level_1_name'],
                                'level_2_id' => $service_classification['level_2_id'],
                                'level_2_name' => $service_classification['level_2_name'],
                                'level_3_id' => $service_classification['level_3_id'],
                                'level_3_name' => $service_classification['level_3_name'],
                                'reteurn_type' => 1,
                            );
                            array_push($order_product_detail,$_order_product_detail);
                        }
                    }else{
                        $_order_product = OrderProduct::find($_roi->order_product_id);
                        $service_detail = Report::get_service_detail($_order_product->product_id, $_order_product->product_type);
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        if($_order_product->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::find($_order_product->engineer_id);
                        }
                        $item = OrderProductAcItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
                        foreach($item as $_it){
                            $item_type = $_it->type;
                            $supply_amount=0;                                
                            if($item_type == 1) {
                                $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                foreach ($before_return as $_br) {
                                    $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                    foreach ($temp_msi as $_ts) {
                                        $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                        $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                    }
                                }
                                $supply_amount = -$supply_amount;
                            }
                        }
                        $_order_product_detail = array(
                            'order_id' => $_ro->order_id,
                            'pay_time' => $_ro->pay_time,
                            'car_no' => $_ro->car_no,
                            'name' => $_ro->name,
                            'order_engineer' => $order_engineer->name,
                            'order_engineer_id' => $order_engineer->id,
                            'product_name' => $_order_product->product_name,
                            'product_engineer' => $product_engineer->name,
                            'product_engineer_id' => $product_engineer->id,
                            'product_amount' => 0,
                            'product_discount' => 0,
                            'counter_amount' => -$_order_product->counter_amount,
                            'supply_amount' => $supply_amount,
                            'level_1_id' => $service_classification['level_1_id'],
                            'level_1_name' => $service_classification['level_1_name'],
                            'level_2_id' => $service_classification['level_2_id'],
                            'level_2_name' => $service_classification['level_2_name'],
                            'level_3_id' => $service_classification['level_3_id'],
                            'level_3_name' => $service_classification['level_3_name'],
                            'reteurn_type' => 1,
                        );
                        array_push($order_product_detail,$_order_product_detail);
                    }
                } else{
                    switch ($_roi->product_type) {
                        case 'enterprise':
                            $order_product_item = OrderProductEsItem::find($_roi->order_product_item_id);
                            $op=OrderProductEsPackage::find($order_product_item->order_product_package_id);
                            $product=ProductPackage::find($op->package_id);
                            $product_name=$op->name;
                            $item_type=$order_product_item->type;
                            $st=$product->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'activity':
                            $order_product_item = OrderProductAcItem::find($_roi->order_product_item_id);
                            $op=OrderProductAcPackage::find($order_product_item->order_product_package_id);
                            $product=ProductPackage::find($op->package_id);
                            $product_name=$op->name;
                            $item_type=$order_product_item->type;
                            $st=$product->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'promotion':
                            break;
                        case 'base':
                            $order_product_item = OrderProductBaseItem::find($_roi->order_product_item_id);
                            $op=OrderProduct::find($order_product_item->order_product_package_id);
                            $product=ProductPackage::find($op->product_id);
                            $product_name=$op->name;
                            $item_type=$order_product_item->type;
                            $st=$product->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'non_standard':
                            $order_product_item = OrderProductNonStandard::find($_roi->order_product_item_id);
                            $op=OrderProduct::find($order_product_item->order_product_id);
                            $product_name=$op->name;
                            $item_type=$order_product_item->service_type;
                            $s=Service::find($op->product_id);
                            $st=$s->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'service_card':
                            $order_product_item = OrderProductScItem::find($_roi->order_product_item_id);
                            $op=OrderProduct::find($order_product_item->order_product_id);
                            $product_name=$op->name;
                            $item_type=$order_product_item->service_type;
                            $s=ProductPackage::find($op->product_id);
                            $st=$s->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                    }
                    $counter_amount=$supply_amount=0;
                    $counter_amount=-$_roi->return_amount;
                    switch ($_roi->type) {
                        case '0'://（退库存）退成本退钱
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("product_type",$_roi->product_type)->where("product_item_id",$_roi->order_product_item_id)->get();

                            foreach ($before_return as $_br) {
                                $temp_msi=MaintenanceStorageItem::where("return_order_id",$_br->id)->where("status",1)->get();
                                foreach ($temp_msi as $_ts) {
                                    $temp_moi=MaintenanceOutItem::find($_ts->out_id);
                                    $supply_amount=$supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                }
                            }
                            $supply_amount=-$supply_amount;
                            break;
                    }
                    if(isset($pacek_arr[$_roi->product_type."_".$op->id])){
                        $temp=$pacek_arr[$_roi->product_type."_".$op->id];
                        $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
                        $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
                        $pacek_arr[$_roi->product_type."_".$op->id]=$temp;
                    }else{
                        $pacek_arr[$_roi->product_type."_".$op->id]=array('product_name'=>$product_name,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,"st"=>$st,"product_engineer_id"=>$product_engineer_id);
                    }
                }
            }
            foreach ($pacek_arr as $_pa) {
                $service_classification = Report::get_service_classification($_pa["st"]);
                if($_pa["product_engineer_id"] == 0){
                    $product_engineer = $order_engineer;
                }else{
                    $product_engineer = MaintenanceEngineers::find($_pa["product_engineer_id"]);
                }
                $_order_product_detail = array(
                    'order_id' => $_ro->order_id,
                    'pay_time' => $_ro->pay_time,
                    'car_no' => $_ro->car_no,
                    'name' => $_ro->name,
                    'order_engineer' => $order_engineer->name,
                    'order_engineer_id' => $order_engineer->id,
                    'product_name' => $_pa['product_name'],
                    'product_engineer' => $product_engineer->name,
                    'product_engineer_id' => $product_engineer->id,
                    'product_amount' => 0,
                    'product_discount' => 0,
                    'counter_amount' => $_pa['counter_amount'],
                    'supply_amount' => $_pa['supply_amount'],
                    'level_1_id' => $service_classification['level_1_id'],
                    'level_1_name' => $service_classification['level_1_name'],
                    'level_2_id' => $service_classification['level_2_id'],
                    'level_2_name' => $service_classification['level_2_name'],
                    'level_3_id' => $service_classification['level_3_id'],
                    'level_3_name' => $service_classification['level_3_name'],
                    'reteurn_type' => 1,
                );
                array_push($order_product_detail,$_order_product_detail);
            }
        }
        return $order_product_detail;
    }

    //企业客户应收账款明细
    public static function get_company_credit_detail($begin_date,$end_date,$shop_id){
        $company_credit_order = OrderPayRecord::join('orders', 'order_pay_record.order_id', '=', 'orders.order_id')
            ->join('users','orders.pay_user_id', '=' ,'users.id')
            ->where('order_pay_record.pay_type',6)
            ->where('order_pay_record.status',1)
            ->where('users.type',2)
            ->where('users.status',1)
            ->whereIn('orders.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('orders.pay_time',array($begin_date,$end_date))
            ->where('orders.shop_id',$shop_id)
            ->where('orders.place',2)
            ->select(array('orders.user_id','users.real_name','orders.order_id','order_pay_record.amount','orders.pay_time','orders.car_no'))
            ->orderBy('orders.user_id','asc')
            ->get();
        $temp_array=array();
        foreach ($company_credit_order as $_cco) {
            $_cco->implement_time="";
            $_cco->return_type=0;
            $temp_array[]=$_cco;
        }

        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->leftJoin('users','orders.pay_user_id', '=' ,'users.id')
            ->leftJoin('return_order_record','return_order_record.return_order_id', '=' ,'return_order.id')
            ->where('users.type',2)
            ->where('return_order_record.pay_type',6)
            ->where('users.status',1)
            ->where('orders.place',2)
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->whereBetween("return_order.implement_time",array($begin_date,$end_date))
            ->select(array('orders.user_id','users.real_name','orders.order_id','return_order_record.amount','orders.pay_time','return_order.implement_time','orders.car_no'))->get();
        foreach ($ro as $_ro) {
            $_ro->return_type=1;
            $_ro->amount=-$_ro->amount;
            $temp_array[]=$_ro;
        }
        //洗车单
        $wash_os = CarWashOrderPayRecord::join('car_wash_order', 'car_wash_order_pay_record.order_id', '=', 'car_wash_order.order_id')
            ->join('users','car_wash_order.user_id', '=' ,'users.id')
            ->where('car_wash_order.status',1417)
            ->where('car_wash_order_pay_record.pay_type',6)
            ->where('car_wash_order_pay_record.status',1)
            ->whereBetween('car_wash_order.pay_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->orderBy('car_wash_order.pay_time',"desc")
            ->where('users.type',2)
            ->where('users.status',1)
            ->select(array(
                'car_wash_order.user_id',
                'users.real_name',
                'car_wash_order.order_id',
                'car_wash_order_pay_record.amount',
               // 'car_wash_order_pay_record.implement_time',
                'car_wash_order.pay_time',
                'car_wash_order.car_no',
            ))
            ->get();
        foreach($wash_os as $wo) {
            $wo->implement_time="";
            $wo->return_type=0;
            $temp_array[]=$wo;
        }
        //洗车单 退货
        $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->leftJoin('users','car_wash_order.user_id', '=' ,'users.id')
            ->leftJoin('return_washorder_record','return_washorder_record.return_washorder_id', '=' ,'return_carwash_order.id')
            ->where('users.type',2)
            ->where('users.status',1)
            ->where('return_washorder_record.pay_type',6)
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->select(DB::raw(
                'car_wash_order.user_id,
                 users.real_name,
                 car_wash_order.order_id,
                 return_washorder_record.amount,
                 return_washorder_record.created_at as implement_time,
                 car_wash_order.pay_time,
                 car_wash_order.car_no'
            ))
            ->get();
        foreach($wash_os_r as $wo_r){
            $wo_r->return_type = 1;
            $wo_r->amount=-$wo_r->amount;
            $temp_array[]=$wo_r;
        }

        //套餐卡单
        $sc_os = ServiceCardOrderPayRecord::join('service_card_order', 'service_card_order_pay_record.order_id', '=', 'service_card_order.order_id')
            ->join('users','service_card_order.user_id', '=' ,'users.id')
            ->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))
            ->where('service_card_order_pay_record.pay_type',6)
            ->where('service_card_order_pay_record.status',1)
            ->whereBetween('service_card_order.pay_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->orderBy('service_card_order.pay_time',"desc")
            ->where('users.type',2)
            ->where('users.status',1)
            ->select(array(
                'service_card_order.user_id',
                'users.real_name',
                'service_card_order.order_id',
                'service_card_order_pay_record.amount',
               // 'service_card_order_pay_record.implement_time',
                'service_card_order.pay_time',
                'service_card_order.car_no',
            ))
            ->get();
        foreach($sc_os as $sc) {
            $sc->implement_time="";
            $sc->return_type=0;
            $temp_array[]=$sc;
        }

        //套餐卡 退货
        $wash_os_r = ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")
            ->leftJoin('users','service_card_order.user_id', '=' ,'users.id')
            ->leftJoin('return_service_card_record','return_service_card_record.return_order_id', '=' ,'return_service_card_order.id')
            ->where('users.type',2)
            ->where('users.status',1)
            ->where('return_service_card_record.pay_type',6)
            ->where('return_service_card_order.status',1)
            ->whereBetween('return_service_card_order.implement_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->select(DB::raw(
                'service_card_order.user_id,
                 users.real_name,
                 service_card_order.order_id,
                 return_service_card_order.amount,
                 return_service_card_order.created_at as implement_time,
                 service_card_order.pay_time,
                 service_card_order.car_no'
            ))
            ->get();
        foreach($wash_os_r as $wo_r){
            $wo_r->return_type = 1;
            $wo_r->amount=-$wo_r->amount;
            $temp_array[]=$wo_r;
        }
        return $temp_array;
    }

    //企业客户应收账款统计
    public static function get_company_credit_sum($begin_date,$end_date,$shop_id){
        $company_credit_order = OrderPayRecord::join('orders', 'order_pay_record.order_id', '=', 'orders.order_id')
            ->join('users','orders.pay_user_id', '=' ,'users.id')
            ->where('order_pay_record.pay_type',6)
            ->where('order_pay_record.status',1)
            ->where('users.type',2)
            ->where('users.status',1)
            ->whereIn('orders.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('orders.pay_time',array($begin_date,$end_date))
            ->where('orders.shop_id',$shop_id)
            ->where('orders.place',2)
            ->select(DB::raw('date(orders.pay_time) as date ,users.real_name as name,users.id as u_id,sum(order_pay_record.amount) as amount,count(*) as num'))
            ->groupBy('date','u_id')
            ->orderBy('orders.user_id','asc')
            ->get();
        $temp_array=array();
        foreach ($company_credit_order as $_cco) {
            $_cco->return_amount=0;
            $temp_array[$_cco->date."_".$_cco->u_id]=$_cco;
        }
        //洗车单
        $wash_os = CarWashOrderPayRecord::join('car_wash_order', 'car_wash_order_pay_record.order_id', '=', 'car_wash_order.order_id')
            ->join('users','car_wash_order.user_id', '=' ,'users.id')
            ->where('car_wash_order_pay_record.pay_type',6)
            ->where('car_wash_order_pay_record.status',1)
            ->where('users.type',2)
            ->where('users.status',1)
            ->where('car_wash_order.status',1417)
            ->whereBetween('car_wash_order.pay_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->orderBy('car_wash_order.pay_time',"desc")
            ->select(DB::raw('date(car_wash_order.pay_time) as date ,users.real_name as name,users.id as u_id,sum(car_wash_order_pay_record.amount) as amount,count(*) as num'))
            ->groupBy('date','u_id')
            ->orderBy('car_wash_order.user_id','asc')
            ->get();
        foreach($wash_os as $wo) {
            $wo->return_amount=0;
            if(isset($temp_array[$wo->date."_".$wo->u_id])){
                $temp_array[$wo->date."_".$wo->u_id]->amount+=$wo->amount;
                $temp_array[$wo->date."_".$wo->u_id]->num+=$wo->num;
            }else {
                $temp_array[$wo->date . "_" . $wo->u_id] = $wo;
            }
        }
        //套餐卡单
        $sc_os = ServiceCardOrderPayRecord::join('service_card_order', 'service_card_order_pay_record.order_id', '=', 'service_card_order.order_id')
            ->join('users','service_card_order.user_id', '=' ,'users.id')
            ->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))
            ->where('service_card_order_pay_record.pay_type',6)
            ->where('service_card_order_pay_record.status',1)
            ->whereBetween('service_card_order.pay_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->where('users.type',2)
            ->where('users.status',1)
            ->select(DB::raw('date(service_card_order.pay_time) as date ,users.real_name as name,users.id as u_id,sum(service_card_order_pay_record.amount) as amount,count(*) as num'))
            ->groupBy('date','u_id')
            ->orderBy('service_card_order.user_id','asc')
            ->get();
        foreach($sc_os as $sc) {
            $sc->return_amount=0;
            if(isset($temp_array[$sc->date."_".$sc->u_id])){
                $temp_array[$sc->date."_".$sc->u_id]->amount+=$sc->amount;
                $temp_array[$sc->date."_".$sc->u_id]->num+=$sc->num;
            }else {
                $temp_array[$sc->date . "_" . $sc->u_id] = $sc;
            }
        }
        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->leftJoin('users','orders.pay_user_id', '=' ,'users.id')
            ->leftJoin('return_order_record','return_order_record.return_order_id', '=' ,'return_order.id')
            ->where('users.type',2)
            ->where('return_order_record.pay_type',6)
            ->where('users.status',1)
            ->where('orders.place',2)
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(DB::raw('date(return_order.implement_time) as date ,users.real_name as name,users.id as u_id,sum(return_order_record.amount) as return_amount'))->groupBy('date','u_id')->get();
        foreach ($ro as $_ro) {
            if(isset($temp_array[$_ro->date."_".$_ro->u_id])){
                $temp_array[$_ro->date."_".$_ro->u_id]->return_amount+=$_ro->return_amount;
            }else{
                $_ro->amount=0;
                $_ro->num=0;
                $temp_array[$_ro->date."_".$_ro->u_id]=$_ro;
            }
        }
        //洗车单 退货
        $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->leftJoin('users','car_wash_order.user_id', '=' ,'users.id')
            ->leftJoin('return_washorder_record','return_washorder_record.return_washorder_id', '=' ,'return_carwash_order.id')
            ->where('users.type',2)
            ->where('users.status',1)
            ->where('return_washorder_record.pay_type',6)
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.created_at',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->select(DB::raw('date(return_carwash_order.implement_time) as date ,users.real_name as name,users.id as u_id,sum(return_washorder_record.amount) as return_amount'))
            ->groupBy('date','u_id')
            ->get();
        foreach($wash_os_r as $wo_r){
            if(isset($temp_array[$wo_r->date."_".$wo_r->u_id])){
                $temp_array[$wo_r->date."_".$wo_r->u_id]->return_amount+=$wo_r->return_amount;
            }else{
                $wo_r->amount=0;
                $wo_r->num=0;
                $temp_array[$wo_r->date."_".$wo_r->u_id]=$wo_r;
            }
        }
        //套餐卡 退货
        $wash_os_r = ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")
            ->leftJoin('users','service_card_order.user_id', '=' ,'users.id')
            ->leftJoin('return_service_card_record','return_service_card_record.return_order_id', '=' ,'return_service_card_order.id')
            ->where('users.type',2)
            ->where('users.status',1)
            ->where('return_service_card_record.pay_type',6)
            ->where('return_service_card_order.status',1)
            ->whereBetween('return_service_card_order.implement_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
             ->select(DB::raw('date(return_service_card_order.implement_time) as date ,users.real_name as name,users.id as u_id,sum(return_service_card_record.amount) as return_amount'))
            ->groupBy('date','u_id')
            ->get();
        // foreach($wash_os_r as $wo_r){
        //     $wo_r->return_type = 1;
        //     $wo_r->amount=-$wo_r->amount;
        //     $temp_array[]=$wo_r;
        // }
        foreach($wash_os_r as $wo_r){
            if(isset($temp_array[$wo_r->date."_".$wo_r->u_id])){
                $temp_array[$wo_r->date."_".$wo_r->u_id]->return_amount+=$wo_r->return_amount;
            }else{
                $wo_r->amount=0;
                $wo_r->num=0;
                $temp_array[$wo_r->date."_".$wo_r->u_id]=$wo_r;
            }
        }

        return array_values($temp_array);
    }

    //订单配件成本统计
    public static function get_order_parts_cost($begin_date,$end_date,$shop_id){
        $order_parts_cost = array();
        $order_array = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->select(array('order_id'))
            ->get()
            ->toArray();
        $maintenance_out_orders = MaintenanceOutOrder::whereIn('status',array(1,2))
            ->whereIn('order_id',$order_array)
            ->select(array('order_id','type','item_id','number','status','id'))
            ->get();
        foreach ($maintenance_out_orders as $_maintenance_out_order){
            $car_no = Orders::where('order_id',$_maintenance_out_order->order_id)->select(array('car_no'))->first();
            $maintenance_out_item = MaintenanceOutItem::where('order_out_id',$_maintenance_out_order->id)
                ->select(array('order_out_id','name','tax_purchase_price','tax_purchase_subtotal','number'))
                ->get()
                ->toArray();
            $_order_parts_cost = array(
                'order_id' => $_maintenance_out_order->order_id,
                'car_no'  => $car_no->car_no,
                'type' => $_maintenance_out_order->type,
                'item_id' => $_maintenance_out_order->item_id,
                'number' => $_maintenance_out_order->number,
                'status' => $_maintenance_out_order->status,
                'id' => $_maintenance_out_order->id,
                'item' => $maintenance_out_item,
            );
            array_push($order_parts_cost,$_order_parts_cost);
        }
        return $order_parts_cost;
    }

    //订单服务信息
    /**
     * @Author:      lyt
     * @DateTime:    2016-07-30 13:51:36
     * @Description: 因该是废弃方法 暂时没发现用处 暂时不做修改
     */
    public static function get_order_services($begin_date,$end_date,$shop_id){
        $order_services = array();
        $order_array = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->select(array('order_id'))
            ->get()
            ->toArray();
        $product_packages = OrderProduct::whereIn('order_id',$order_array)
            ->where('status',1)
            ->get();
        foreach ($product_packages as $_product){
            if($_product->product_type=='enterprise'){//说明是 企业包
                $opep=OrderProductEsPackage::where("order_product_id",$_product->id)->get();
                foreach ($opep as $_opep){
                    $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                    $service_classification = Report::get_service_classification($service_detail->classify);
                    $item=OrderProductEsItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                    if($_opep->engineer_id == 0){
                        $order = Orders::where('order_id',$_product->order_id)->select(array('engineer_id'))->first();
                        $engineer_id = $order->engineer_id;
                    }else{
                        $engineer_id = $_product->engineer_id;
                    }
                    $engineer_info = Report::get_engineer_info($engineer_id);
                    $_order_services = array(
                        'order_id' => $_product->order_id,
                        'status' => $_product->status,
                        'engineer_id' => $engineer_id,
                        'engineer_name' => isset($engineer_info)?$engineer_info->name:'',
                        'product_id' => $_opep->product_id,
                        'product_type' => $_product->product_type,
                        'st_classifi_id' => $service_detail->classify,
                        'level_1_id' => $service_classification['level_1_id'],
                        'level_1_name' => $service_classification['level_1_name'],
                        'level_2_id' => $service_classification['level_2_id'],
                        'level_2_name' => $service_classification['level_2_name'],
                        'level_3_id' => $service_classification['level_3_id'],
                        'level_3_name' => $service_classification['level_3_name'],
                    );
                    array_push($order_services,$_order_services);
                }
            }elseif($_product->product_type=='activity'){//说明是 活动包
                $opep=OrderProductAcPackage::where("order_product_id",$_product->id)->get();
                foreach ($opep as $_opep){
                    $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                    $service_classification = Report::get_service_classification($service_detail->classify);
                    $item=OrderProductAcItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                    if($_opep->engineer_id == 0){
                        $order = Orders::where('order_id',$_product->order_id)->select(array('engineer_id'))->first();
                        $engineer_id = $order->engineer_id;
                    }else{
                        $engineer_id = $_product->engineer_id;
                    }
                    $engineer_info = Report::get_engineer_info($engineer_id);
                    $_order_services = array(
                        'order_id' => $_product->order_id,
                        'status' => $_product->status,
                        'engineer_id' => $engineer_id,
                        'engineer_name' => isset($engineer_info)?$engineer_info->name:'',
                        'product_id' => $_opep->product_id,
                        'product_type' => $_product->product_type,
                        'st_classifi_id' => $service_detail->classify,
                        'level_1_id' => $service_classification['level_1_id'],
                        'level_1_name' => $service_classification['level_1_name'],
                        'level_2_id' => $service_classification['level_2_id'],
                        'level_2_name' => $service_classification['level_2_name'],
                        'level_3_id' => $service_classification['level_3_id'],
                        'level_3_name' => $service_classification['level_3_name'],
                    );
                    array_push($order_services,$_order_services);
                }
            }else{
                $service_detail = Report::get_service_detail($_product->product_id,$_product->product_type);
                $service_classification = Report::get_service_classification($service_detail->classify);
                if($_product->engineer_id == 0){
                    $order = Orders::where('order_id',$_product->order_id)->select(array('engineer_id'))->first();
                    $engineer_id = $order->engineer_id;
                }else{
                    $engineer_id = $_product->engineer_id;
                }
                $engineer_info = Report::get_engineer_info($engineer_id);
                $_order_services = array(
                    'order_id' => $_product->order_id,
                    'status' => $_product->status,
                    'engineer_id' => $engineer_id,
                    'engineer_name' => isset($engineer_info)?$engineer_info->name:'',
                    'product_id' => $_product->product_id,
                    'product_type' => $_product->product_type,
                    'st_classifi_id' => $service_detail->classify,
                    'level_1_id' => $service_classification['level_1_id'],
                    'level_1_name' => $service_classification['level_1_name'],
                    'level_2_id' => $service_classification['level_2_id'],
                    'level_2_name' => $service_classification['level_2_name'],
                    'level_3_id' => $service_classification['level_3_id'],
                    'level_3_name' => $service_classification['level_3_name'],
                );
                array_push($order_services,$_order_services);
            }
        }
        //洗车单
        $wash_os = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach($wash_os as $wo){
            $pp = ProductPackage::find($wo->producd_id);
            $ppi=ProductPackItem::where("pack_id",$wo->producd_id)->where("status",1)->where("type",2)->first();
            $spi=ServiceProductItem::find($ppi->item_id);
            $sc = Report::get_service_classification($pp->classify);
            if(!is_null($wo->engineer_id)) {
                $order_engineer = MaintenanceEngineers::where('id', $wo->engineer_id)->where('status', 1)->first();
                if($order_engineer) {
                    $en_name = $order_engineer->name;
                }else{
                    $en_name = "";
                }
            }else{
                $en_name = "";
            }
            $_work_hour_report = array(
                'order_id' => $wo->order_id, //派工单号
                'status' => $wo->status,    //状态
                'engineer_id' => $wo->engineer_id,   //订单技师
                'engineer_name' => $en_name,   //订单技师
                'product_id' => $wo->producd_id,    //
                'product_type' => $pp->product_type,   //
                'st_classifi_id' => $pp->classify,   //
                'level_1_id' => $sc['level_1_id'],
                'level_1_name' => $sc['level_1_name'],
                'level_2_id' => $sc['level_2_id'],
                'level_2_name' => $sc['level_2_name'],
                'level_3_id' => $sc['level_3_id'],
                'level_3_name' => $sc['level_3_name'],
            );
            array_push($order_services,$_work_hour_report);
        }
        return $order_services;
    }

    //订单配件信息
    public static function get_order_parts($begin_date,$end_date,$shop_id){
        $order_parts = array();
        $order_array = Orders::whereIn('status',array(1018,1017,1217,1317,1417))

            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->select(array('order_id'))
            ->get()
            ->toArray();
        //查找领料 或者退货的  因为 存在结算后退货的情况
        $part_items = MaintenanceOutOrder::whereIn('order_id',$order_array)
            ->whereIn('status',array(2,3))
            ->get();
        foreach ($part_items as $_part_item){
           // if($_part_item->type==0){
           //     $item_info=SupplyItem::getCacheItemInfo($_part_item->item_id);
           // }else{
           //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_part_item->item_id);
           // }
            $item_info = SupplyItem::find($_part_item->item_id);
            //结算后退货的
            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_part_item->id)->where("status",1)->sum("number");
            if($before_return==0&&$_part_item->status==3){//如果是 这个 批次是结算前 退货完成的 不统计
                continue;
            }
           // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
            $_order_parts = array(
                'order_id' => $_part_item->order_id,
                'status' => $_part_item->status,
                'number' => $_part_item->number,
                'return_number' => $_part_item->return_number-$before_return,
                'type' => $_part_item->type,
                'item_id' => $item_info->id,
                'item_brand_name' => $item_info->brand_name,
                'item_series_name' => $item_info->series_name,
                'item_model' => $item_info->model,
                'item_code' => $item_info->code,
                'item_name' => $item_info->name,
                'item_specification' => $item_info->specification,
                'item_unit' => $item_info->unit,
                'item_bz_unit' => $item_info->bz_unit,
                'item_pack' => $item_info->volume_size,
                'st_classifi_id' => $item_info->supply_classification_id,
                'level_1_id' => $part_classification['level_1_id'],
                'level_1_name' => $part_classification['level_1_name'],
                'level_2_id' => $part_classification['level_2_id'],
                'level_2_name' => $part_classification['level_2_name'],
                'level_3_id' => $part_classification['level_3_id'],
                'level_3_name' => $part_classification['level_3_name'],
                'return_type' => 0,
            );
            array_push($order_parts,$_order_parts);
        }

        //获取这段时间退货的
        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id'))->get()->toArray();
        $return_end=MaintenanceReturnOrder::where("source",'return_order')->whereIn("return_order_id",$ro)->where("status",1)->get();
        foreach ($return_end as $_rn) {
            $moo=MaintenanceOutOrder::find($_rn->out_order_id);
           // if($_rn->type==0){
           //     $item_info=SupplyItem::getCacheItemInfo($_rn->item_id);
           // }else{
           //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_rn->item_id);
           // }
            $item_info = SupplyItem::find($_rn->item_id);
           // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
            $_order_parts = array(
                'order_id' => $_rn->order_id,
                'status' => -1,
                'number' => $moo->number,
                'return_number' => $_rn->number,
                'type' => $_rn->type,
                'item_id' => $item_info->id,
                'item_brand_name' => $item_info->brand_name,
                'item_series_name' => $item_info->series_name,
                'item_model' => $item_info->model,
                'item_code' => $item_info->code,
                'item_name' => $item_info->name,
                'item_specification' => $item_info->specification,
                'item_unit' => $item_info->unit,
                'item_bz_unit' => $item_info->bz_unit,
                'item_pack' => $item_info->volume_size,
                'st_classifi_id' => $item_info->supply_classification_id,
                'level_1_id' => $part_classification['level_1_id'],
                'level_1_name' => $part_classification['level_1_name'],
                'level_2_id' => $part_classification['level_2_id'],
                'level_2_name' => $part_classification['level_2_name'],
                'level_3_id' => $part_classification['level_3_id'],
                'level_3_name' => $part_classification['level_3_name'],
                'return_type' => 1,
            );
            array_push($order_parts,$_order_parts);
        }
        return $order_parts;
    }

    //订单结算信息
    public static function get_order_income($begin_date,$end_date,$shop_id){
        $total_order=array();
        $order = Orders::leftJoin('users', 'orders.user_id', '=', 'users.id')
            ->whereIn('orders.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('orders.pay_time',array($begin_date,$end_date))
            ->where('orders.shop_id',$shop_id)
            ->where('orders.place',2)
            ->select(array(
                'orders.order_id',
                'users.phone',
                'orders.car_no',
                'orders.name',
                'users.type',
                'orders.pay_time',
                'orders.amount',
                'orders.discount_amount',
                'orders.payable_amount',
                'orders.order_type',
                'orders.pay_type',
                'orders.status'
            ))->orderBy('orders.pay_time',"asc")
            ->get();
        foreach ($order as $_order) {
            $order_pay=OrderPay::where("order_id",$_order->order_id)->where("status",1)->select("pay_type","amount")->get();
            $_order->pay_details=$order_pay;
            $_order->return_type=0;
            $total_order[]=$_order;
        }
        //洗车单
        $wash_os = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach($wash_os as $wo){
            $std = new stdClass();
            $std->data_type = 'wash_order';
            $std->order_id = $wo->order_id;
            $std->phone = $wo->phone;
            $std->car_no = $wo->car_no;
            $std->name = '';
            $std->type = '';
            $std->pay_time = $wo->pay_time;
            $std->amount = $wo->amount;
            $std->discount_amount = $wo->dis_amount;
            $std->payable_amount = $wo->pay_amount;
            $std->order_type = 3;
            $std->pay_type = $wo->pay_type;
            $std->status = $wo->status;
            $wo_pay = CarWashOrderPayRecord::where("order_id",$wo->order_id)->where("status",1)->select("pay_type","amount")->get();
            $std->pay_details = $wo_pay;
            $std->return_type = 0;
            $total_order[]=$std;
        }
        //套餐卡单
        $sc_order = ServiceCardOrder::leftJoin('users', 'service_card_order.user_id', '=', 'users.id')
            ->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('service_card_order.pay_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->select(array(
                'service_card_order.order_id',
                'users.phone',
                'service_card_order.car_no',
                'users.real_name',
                'users.type',
                'service_card_order.pay_time',
                'service_card_order.amount',
                // 'orders.discount_amount',
                'service_card_order.pay_amount',
                // 'orders.order_type',
                'service_card_order.pay_type',
                'service_card_order.pay_time',
                'service_card_order.status'
            ))->orderBy('service_card_order.pay_time',"asc")
            ->get();
        foreach($sc_order as $sc){
            $std = new stdClass();
            $std->data_type = 'sc_order';
            $std->order_id = $sc->order_id;
            $std->phone = $sc->phone;
            $std->car_no = $sc->car_no;
            $std->name = $sc->real_name;
            $std->type = $sc->type;
            $std->pay_time = $sc->pay_time;
            $std->amount = $sc->amount;
            $std->discount_amount = 0;
            $std->payable_amount = $sc->pay_amount;
            $std->order_type = 4;
            $std->pay_type = $sc->pay_type;
            $std->status = $sc->status;
            $sc_pay = ServiceCardOrderPayRecord::where("order_id",$sc->order_id)->where("status",1)->select("pay_type","amount")->get();
            $std->pay_details = $sc_pay;
            $std->return_type = 0;
            $total_order[]=$std;
        }
        //退货的记录
        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->leftJoin('users', 'orders.user_id', '=', 'users.id')
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($begin_date,$end_date))
            ->select(array('return_order.id',
                'orders.order_id',
                'users.phone',
                'orders.car_no',
                'orders.name',
                'users.type',
                'orders.pay_time',
                'orders.amount',
                'orders.discount_amount',
                'return_order.amount as payable_amount',
                'orders.order_type',
                'orders.pay_type',
                'orders.status'))->orderBy('return_order.implement_time',"asc")->get();
        foreach ($ro as $_ro) {
            // $order_pay=OrderPay::where("order_id",$_order->order_id)->where("status",1)->select("pay_type","amount")->get();
            $order_pay=ReturnOrderRecord::where("return_order_id",$_ro->id)->select("pay_type","amount")->get();
            $_ro->pay_details=$order_pay;
            $_ro->payable_amount=-$_ro->payable_amount;
            $_ro->return_type=1;
            $total_order[]=$_ro;
        }
        //洗车单 退货
        $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->select(array('return_carwash_order.id',"car_wash_order.*"))
            ->get();
        foreach($wash_os_r as $wo_r){
            $std = new stdClass();
            $std->data_type = 'wash_order_return';
            $std->id = $wo_r->id;
            $std->order_id = $wo_r->order_id;
            $std->phone = $wo_r->phone;
            $std->car_no = $wo_r->car_no;
            $std->name = '';
            $std->type = '';
            $std->pay_time = $wo_r->pay_time;
            $std->amount = $wo_r->amount;
            $std->discount_amount = $wo_r->dis_amount;
            $std->payable_amount = -$wo_r->pay_amount;
            $std->order_type = 3;
            $std->pay_type = $wo_r->pay_type;
            $std->status = $wo_r->status;
            $wor_pay = ReturnWashOrderRecord::where("return_washorder_id",$wo_r->id)->select("pay_type","amount")->get();
            $std->pay_details = $wor_pay;
            $std->return_type = 1;
            $total_order[]=$std;
        }

        //套餐卡 退货
        $wash_os_r = ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")
            ->leftJoin('users', 'service_card_order.user_id', '=', 'users.id')
            ->where('return_service_card_order.status',1)
            ->whereBetween('return_service_card_order.implement_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->select(array('return_service_card_order.id','return_service_card_order.amount as return_pay_amount',"service_card_order.*","users.phone","users.real_name","users.type"))
            ->get();
        foreach($wash_os_r as $wo_r){
            $std = new stdClass();
            $std->data_type = 'sc_order_return';
            $std->id = $wo_r->id;
            $std->order_id = $wo_r->order_id;
            $std->phone = $wo_r->phone;
            $std->car_no = $wo_r->car_no;
            $std->name = $wo_r->real_name;
            $std->type = $wo_r->type;
            $std->pay_time = $wo_r->pay_time;
            $std->amount = $wo_r->amount;
            $std->discount_amount = 0;
            $std->payable_amount = -$wo_r->return_pay_amount;
            $std->order_type = 4;
            $std->pay_type = $wo_r->pay_type;
            $std->status = $wo_r->status;
            $wor_pay = ReturnServiceCardRecord::where("return_order_id",$wo_r->id)->select("pay_type","amount")->get();
            $std->pay_details = $wor_pay;
            $std->return_type = 1;
            $total_order[]=$std;
        }
        return $total_order;
    }

    //结算工时分类统计
    public static function get_work_hour_report($begin_date,$end_date,$shop_id){
        $work_hour_report = array();
        $order = Orders::whereIn('status',array(1017,1217,1317,1417,1018))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach ($order as $_order){
            $user_car_info = UserCarInfo::where('id',$_order->user_car_id)->first();
            $order_product = OrderProduct::where('order_id',$_order->order_id)
                ->where('status',1)
                ->get();
            foreach ($order_product as $_order_product){
                if($_order_product->product_type=='enterprise'){//说明是 企业报
                    $opep=OrderProductEsPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        $item=OrderProductEsItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                        if(!is_null($item)){
                            foreach ($item as $_item){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;

                                $order_engineer = MaintenanceEngineers::where('id',$_order->engineer_id)->first();
                                if($_opep->engineer_id == 0){
                                    $product_engineer = $order_engineer;
                                }else{
                                    $product_engineer = MaintenanceEngineers::where('id',$_opep->engineer_id)->first();
                                }
                                $_work_hour_report = array(
                                    'order_id' => $_order->order_id, //派工单号
                                    'pay_time' => $_order->pay_time,   //结算日期
                                    'name' => $_order->name,   //客户名称
                                    'car_no' => $_order->car_no,    //车牌号
                                    'status' => $_order->status,    //状态
                                    'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                    'car_brand_name' => $_order->car_brand_name,    //车辆品牌
                                    'car_series_name' => $_order->car_series_name,    //车辆系列
                                    'car_model_name' => $_order->car_model_name,    //车辆型号
                                    'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                    'service_id' => $service_id,    //工时编码
                                    'item_name' => $_item->item_name,   //名称
                                    'amount' => $_item->amount,   //工时费
                                    'discount' => 0,    //折扣
                                    'real_amount' => $_item->amount,  //实际费用
                                    'counter_amount' => $_item->counter_amount,  //减免实收
                                    'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                    //班组
                                    //工时类型
                                    'level_1_id' => $service_classification['level_1_id'],
                                    'level_1_name' => $service_classification['level_1_name'],
                                    'level_2_id' => $service_classification['level_2_id'],
                                    'level_2_name' => $service_classification['level_2_name'],
                                    'level_3_id' => $service_classification['level_3_id'],
                                    'level_3_name' => $service_classification['level_3_name'],
                                    //是否收费
                                    'work_type' => $work_type,    //工时定额
                                    'work_hours' => $work_hours,    //工时
                                    //接车员
                                    //品牌
                                );
                                array_push($work_hour_report,$_work_hour_report);
                            }
                        }
                    }
                }elseif($_order_product->product_type=='activity'){//说明是 活动包
                    $opep=OrderProductAcPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        $item=OrderProductAcItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                        if(!is_null($item)){
                            foreach ($item as $_item){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;

                                $order_engineer = MaintenanceEngineers::where('id',$_order->engineer_id)->first();
                                if($_opep->engineer_id == 0){
                                    $product_engineer = $order_engineer;
                                }else{
                                    $product_engineer = MaintenanceEngineers::where('id',$_opep->engineer_id)->first();
                                }
                                $_work_hour_report = array(
                                    'order_id' => $_order->order_id, //派工单号
                                    'pay_time' => $_order->pay_time,   //结算日期
                                    'name' => $_order->name,   //客户名称
                                    'car_no' => $_order->car_no,    //车牌号
                                    'status' => $_order->status,    //状态
                                    'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                    'car_brand_name' => $_order->car_brand_name,    //车辆品牌
                                    'car_series_name' => $_order->car_series_name,    //车辆系列
                                    'car_model_name' => $_order->car_model_name,    //车辆型号
                                    'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                    'service_id' => $service_id,    //工时编码
                                    'item_name' => $_item->item_name,   //名称
                                    'amount' => $_item->amount,   //工时费
                                    'discount' => 0,    //折扣
                                    'real_amount' => $_item->amount,  //实际费用
                                    'counter_amount' => $_item->counter_amount,  //减免实收
                                    'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                    //班组
                                    //工时类型
                                    'level_1_id' => $service_classification['level_1_id'],
                                    'level_1_name' => $service_classification['level_1_name'],
                                    'level_2_id' => $service_classification['level_2_id'],
                                    'level_2_name' => $service_classification['level_2_name'],
                                    'level_3_id' => $service_classification['level_3_id'],
                                    'level_3_name' => $service_classification['level_3_name'],
                                    //是否收费
                                    'work_type' => $work_type,    //工时定额
                                    'work_hours' => $work_hours,    //工时
                                    //接车员
                                    //品牌
                                );
                                array_push($work_hour_report,$_work_hour_report);
                            }
                        }
                    }
                }else{
                    if($_order_product->product_type == 'base'){
                        $item = OrderProductBaseItem::where('order_product_package_id',$_order_product->id)
                            ->where('type',2)
                            ->get();
                    }elseif($_order_product->product_type == 'non_standard'){
                        $item = OrderProductNonStandard::where('order_product_id',$_order_product->id)
                            ->where('service_type',2)
                            ->where('status',1)
                            ->get();
                    }elseif($_order_product->product_type == 'service_card'){
                        $item = OrderProductScItem::where('order_product_id',$_order_product->id)
                            ->where('type',2)
                            ->get();
                    }else{
                        $item = null;
                        continue;
                    }
                    $service_detail = Report::get_service_detail($_order_product->product_id, $_order_product->product_type);
                    $service_classification = Report::get_service_classification($service_detail->classify);
                    if(!is_null($item)){
                        foreach ($item as $_item){
                            if($_order_product->product_type == 'base'){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;
                            }elseif($_order_product->product_type == 'non_standard'){
                                $service_id = 0;
                                $work_type = $_item->num;
                                $work_hours = $_item->amount;
                            }elseif($_order_product->product_type == 'service_card'){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;
                            }else{
                                $service_id = 0;
                                $work_type = 0;
                                $work_hours = 0;
                            }
                            $order_engineer = MaintenanceEngineers::where('id',$_order->engineer_id)->first();
                            if($_order_product->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::where('id',$_order_product->engineer_id)->first();
                            }
                            $_work_hour_report = array(
                                'order_id' => $_order->order_id, //派工单号
                                'pay_time' => $_order->pay_time,   //结算日期
                                'name' => $_order->name,   //客户名称
                                'car_no' => $_order->car_no,    //车牌号
                                'status' => $_order->status,    //状态
                                'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                'car_brand_name' => $_order->car_brand_name,    //车辆品牌
                                'car_series_name' => $_order->car_series_name,    //车辆系列
                                'car_model_name' => $_order->car_model_name,    //车辆型号
                                'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                'service_id' => $service_id,    //工时编码
                                'item_name' => $_item->item_name,   //名称
                                'amount' => $_item->amount,   //工时费
                                'discount' => $_item->discount,    //折扣
                                'real_amount' => $_item->amount * $_item->discount,  //实际费用
                                'counter_amount' => $_item->counter_amount,  //减免实收
                                'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                //班组
                                //工时类型
                                'level_1_id' => $service_classification['level_1_id'],
                                'level_1_name' => $service_classification['level_1_name'],
                                'level_2_id' => $service_classification['level_2_id'],
                                'level_2_name' => $service_classification['level_2_name'],
                                'level_3_id' => $service_classification['level_3_id'],
                                'level_3_name' => $service_classification['level_3_name'],
                                //是否收费
                                'work_type' => $work_type,    //工时定额
                                'work_hours' => $work_hours,    //工时
                                //接车员
                                //品牌
                            );
                            array_push($work_hour_report,$_work_hour_report);
                        }
                    }

                }
            }
        }
        //洗车单
        $wash_os = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach($wash_os as $wo){
            $pp = ProductPackage::find($wo->producd_id);
            $ppi=ProductPackItem::where("pack_id",$wo->producd_id)->where("status",1)->where("type",2)->first();
            $spi=ServiceProductItem::find($ppi->item_id);
            $sc = Report::get_service_classification($pp->classify);
            if(!is_null($wo->engineer_id)) {
                $order_engineer = MaintenanceEngineers::where('id', $wo->engineer_id)->first();
                if($order_engineer) {
                    $en_name = $order_engineer->name;
                }else{
                    $en_name = "";
                }
            }else{
                $en_name = "";
            }
            $_work_hour_report = array(
                'order_id' => $wo->order_id, //派工单号
                'pay_time' => $wo->pay_time,   //下单日期
                'name' => '',   //客户名称
                'car_no' => $wo->car_no,    //车牌号
                'status' => $wo->status,    //状态
                'engineer_id' => $en_name,   //订单技师
                'car_brand_name' => '',    //车辆品牌
                'car_series_name' => '',    //车辆系列
                'car_model_name' => '',    //车辆型号
                'vin_code' => '',  //VIN号
                'service_id' => $spi->id,    //工时编码
                'item_name' => $spi->name,   //名称
                'amount' => $wo->amount,   //工时费
                'discount' => 1,    //折扣
                'real_amount' => $wo->amount,  //实际费用
                'counter_amount' => $wo->pay_amount,  //减免实收.

                'maintence_engineer_id' => $en_name,    //修理工
                //班组
                //工时类型
                'level_1_id' => $sc['level_1_id'],
                'level_1_name' => $sc['level_1_name'],
                'level_2_id' => $sc['level_2_id'],
                'level_2_name' => $sc['level_2_name'],
                'level_3_id' => $sc['level_3_id'],
                'level_3_name' => $sc['level_3_name'],
                //是否收费
                'work_type' => $spi->type,    //工时定额
                'work_hours' => $spi->work_hours,    //工时
                //接车员
                //品牌
            );
            array_push($work_hour_report,$_work_hour_report);
        }
        //计算这个时间段内 退货的 记录

        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id',"orders.*"))->orderBy("return_order.implement_time","asc")->get();
        foreach ($ro as $_ro){
            $order_engineer = MaintenanceEngineers::find($_ro->engineer_id);
            $user_car_info = UserCarInfo::where('id',$_ro->user_car_id)->first();
            $roi=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->where("type",3)->first();//说明是 均摊的退货
            if($roi){
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",3)->where("type",3)->get();
            }else{
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->whereIn("type",array(2,4))->get();//
            }
            foreach ($ori as $_roi) {
                if($_roi->type==4){
                    if($_roi->product_type=='enterprise'){//说明是 企业报
                        $opep=OrderProductEsPackage::where("order_product_id",$_roi->order_product_id)->get();
                        foreach ($opep as $_opep){
                            $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                            $service_classification = Report::get_service_classification($service_detail->classify);
                            $item=OrderProductEsItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                            if($_opep->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::where('id',$_opep->engineer_id)->first();
                            }
                            if(!is_null($item)){
                                foreach ($item as $_item){
                                    $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                    $service_id = $service_product_item->id;
                                    $work_type = $service_product_item->type;
                                    $work_hours = $service_product_item->work_hours;
                                    $_work_hour_report = array(
                                        'order_id' => $_ro->order_id, //派工单号
                                        'pay_time' => $_ro->pay_time,   //结算日期
                                        'name' => $_ro->name,   //客户名称
                                        'car_no' => $_ro->car_no,    //车牌号
                                        'status' => $_ro->status,    //状态
                                        'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                        'car_brand_name' => $_ro->car_brand_name,    //车辆品牌
                                        'car_series_name' => $_ro->car_series_name,    //车辆系列
                                        'car_model_name' => $_ro->car_model_name,    //车辆型号
                                        'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                        'service_id' => $service_id,    //工时编码
                                        'item_name' => $_item->item_name,   //名称
                                        'amount' => $_item->amount,   //工时费
                                        'discount' => 0,    //折扣
                                        'real_amount' => $_item->amount,  //实际费用
                                        'counter_amount' => -$_item->counter_amount,  //减免实收
                                        'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                        //班组
                                        //工时类型
                                        'level_1_id' => $service_classification['level_1_id'],
                                        'level_1_name' => $service_classification['level_1_name'],
                                        'level_2_id' => $service_classification['level_2_id'],
                                        'level_2_name' => $service_classification['level_2_name'],
                                        'level_3_id' => $service_classification['level_3_id'],
                                        'level_3_name' => $service_classification['level_3_name'],
                                        //是否收费
                                        'work_type' => $work_type,    //工时定额
                                        'work_hours' => $work_hours,    //工时
                                        //接车员
                                        //品牌
                                    );
                                    array_push($work_hour_report,$_work_hour_report);
                                }
                            }
                        }
                    }elseif($_roi->product_type=='activity'){//说明是 活动包
                        $opep=OrderProductAcPackage::where("order_product_id",$_roi->order_product_id)->get();
                        foreach ($opep as $_opep){
                            if($_opep->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::where('id',$_opep->engineer_id)->first();
                            }
                            $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                            $service_classification = Report::get_service_classification($service_detail->classify);
                            $item=OrderProductAcItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                            if(!is_null($item)){
                                foreach ($item as $_item){
                                    $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                    $service_id = $service_product_item->id;
                                    $work_type = $service_product_item->type;
                                    $work_hours = $service_product_item->work_hours;
                                    $_work_hour_report = array(
                                        'order_id' => $_ro->order_id, //派工单号
                                        'pay_time' => $_ro->pay_time,   //结算日期
                                        'name' => $_ro->name,   //客户名称
                                        'car_no' => $_ro->car_no,    //车牌号
                                        'status' => $_ro->status,    //状态
                                        'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                        'car_brand_name' => $_ro->car_brand_name,    //车辆品牌
                                        'car_series_name' => $_ro->car_series_name,    //车辆系列
                                        'car_model_name' => $_ro->car_model_name,    //车辆型号
                                        'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                        'service_id' => $service_id,    //工时编码
                                        'item_name' => $_item->item_name,   //名称
                                        'amount' => $_item->amount,   //工时费
                                        'discount' => 0,    //折扣
                                        'real_amount' => $_item->amount,  //实际费用
                                        'counter_amount' => -$_item->counter_amount,  //减免实收
                                        'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                        //班组
                                        //工时类型
                                        'level_1_id' => $service_classification['level_1_id'],
                                        'level_1_name' => $service_classification['level_1_name'],
                                        'level_2_id' => $service_classification['level_2_id'],
                                        'level_2_name' => $service_classification['level_2_name'],
                                        'level_3_id' => $service_classification['level_3_id'],
                                        'level_3_name' => $service_classification['level_3_name'],
                                        //是否收费
                                        'work_type' => $work_type,    //工时定额
                                        'work_hours' => $work_hours,    //工时
                                        //接车员
                                        //品牌
                                    );
                                    array_push($work_hour_report,$_work_hour_report);
                                }
                            }
                        }
                    }else{
                        $_order_product = OrderProduct::find($_roi->order_product_id);
                        if($_roi->product_type == 'base'){
                            $item = OrderProductBaseItem::where('order_product_package_id',$_order_product->id)
                                ->where('type',2)
                                ->get();
                        }elseif($_roi->product_type == 'non_standard'){
                            $item = OrderProductNonStandard::where('order_product_id',$_order_product->id)
                                ->where('service_type',2)
                                ->where('status',1)
                                ->get();
                        }elseif($_roi->product_type == 'service_card'){
                            $item = OrderProductScItem::where('order_product_id',$_order_product->id)
                                ->where('type',2)
                                ->get();
                        }else{
                            $item = null;
                            continue;
                        }
                        if($_order_product->engineer_id == 0){
                            $product_engineer = $order_engineer;
                        }else{
                            $product_engineer = MaintenanceEngineers::where('id',$_order_product->engineer_id)->first();
                        }
                        $service_detail = Report::get_service_detail($_order_product->product_id, $_order_product->product_type);
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        if(!is_null($item)){
                            foreach ($item as $_item){
                                if($_order_product->product_type == 'base'){
                                    $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                    $service_id = $service_product_item->id;
                                    $work_type = $service_product_item->type;
                                    $work_hours = $service_product_item->work_hours;
                                }elseif($_order_product->product_type == 'non_standard'){
                                    $service_id = 0;
                                    $work_type = $_item->num;
                                    $work_hours = $_item->amount;
                                }elseif($_order_product->product_type == 'service_card'){
                                    $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                    $service_id = $service_product_item->id;
                                    $work_type = $service_product_item->type;
                                    $work_hours = $service_product_item->work_hours;
                                }else{
                                    $service_id = 0;
                                    $work_type = 0;
                                    $work_hours = 0;
                                }

                                $_work_hour_report = array(
                                    'order_id' => $_ro->order_id, //派工单号
                                    'pay_time' => $_ro->pay_time,   //结算日期
                                    'name' => $_ro->name,   //客户名称
                                    'car_no' => $_ro->car_no,    //车牌号
                                    'status' => $_ro->status,    //状态
                                    'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                    'car_brand_name' => $_ro->car_brand_name,    //车辆品牌
                                    'car_series_name' => $_ro->car_series_name,    //车辆系列
                                    'car_model_name' => $_ro->car_model_name,    //车辆型号
                                    'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                    'service_id' => $service_id,    //工时编码
                                    'item_name' => $_item->item_name,   //名称
                                    'amount' => $_item->amount,   //工时费
                                    'discount' => 0,    //折扣
                                    'real_amount' => $_item->amount,  //实际费用
                                    'counter_amount' => -$_item->counter_amount,  //减免实收
                                    'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                    //班组
                                    //工时类型
                                    'level_1_id' => $service_classification['level_1_id'],
                                    'level_1_name' => $service_classification['level_1_name'],
                                    'level_2_id' => $service_classification['level_2_id'],
                                    'level_2_name' => $service_classification['level_2_name'],
                                    'level_3_id' => $service_classification['level_3_id'],
                                    'level_3_name' => $service_classification['level_3_name'],
                                    //是否收费
                                    'work_type' => $work_type,    //工时定额
                                    'work_hours' => $work_hours,    //工时
                                    //接车员
                                    //品牌
                                );
                                array_push($work_hour_report,$_work_hour_report);
                            }
                        }

                    }
                }else{
                    $service_id = 0;
                    $work_type = 0;
                    $work_hours = 0;
                    switch ($_roi->product_type) {
                        case 'enterprise':
                            $order_product_item = OrderProductEsItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==1){
                                continue 2;
                            }
                            $service_product_item = ServiceProductItem::where('id',$order_product_item->item_id)->first();
                            $service_id = $service_product_item->id;
                            $work_type = $service_product_item->type;
                            $work_hours = $service_product_item->work_hours;
                            $opep=OrderProductEsPackage::find($order_product_item->order_product_package_id);
                            $product=ProductPackage::find($opep->package_id);
                            $st=$product->classify;
                            $product_engineer_id=$opep->engineer_id;
                            break;
                        case 'activity':
                            $order_product_item = OrderProductAcItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==1){
                                continue 2;
                            }
                            $service_product_item = ServiceProductItem::where('id',$order_product_item->item_id)->first();
                            $service_id = $service_product_item->id;
                            $work_type = $service_product_item->type;
                            $work_hours = $service_product_item->work_hours;
                            $opep=OrderProductAcPackage::find($order_product_item->order_product_package_id);
                            $product=ProductPackage::find($opep->package_id);
                            $st=$product->classify;
                            $product_engineer_id=$opep->engineer_id;
                            break;
                        case 'promotion':
                            // $order_product_item = OrderProductItem::find($_roi->order_product_item_id);
                            // $opep=OrderProductPackage::find($order_product_item->order_product_package_id);
                            // $product=ProductPackage::find($opep->package_id);
                            // $st=$product->classify;
                            // $product_engineer_id=$opep->engineer_id;
                            break;
                        case 'base':
                            $order_product_item = OrderProductBaseItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==1){
                                continue 2;
                            }
                            $service_product_item = ServiceProductItem::where('id',$order_product_item->item_id)->first();
                            $service_id = $service_product_item->id;
                            $work_type = $service_product_item->type;
                            $work_hours = $service_product_item->work_hours;
                            $op=OrderProduct::find($order_product_item->order_product_package_id);
                            $product=ProductPackage::find($op->product_id);
                            $st=$product->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'non_standard':
                            $order_product_item = OrderProductNonStandard::find($_roi->order_product_item_id);
                            if($order_product_item->service_type==1){
                                continue 2;
                            }
                            $service_id = 0;
                            $work_type = $order_product_item->num;
                            $work_hours = $order_product_item->amount;
                            $op=OrderProduct::find($order_product_item->order_product_id);
                            $s=Service::find($op->product_id);
                            $st=$s->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                        case 'service_card':
                            $order_product_item = ServiceProductItem::find($_roi->order_product_item_id);
                            if($order_product_item->type==1){
                                continue 2;
                            }
                            $service_product_item = ServiceProductItem::where('id',$order_product_item->item_id)->first();
                            $service_id = $service_product_item->id;
                            $work_type = $service_product_item->type;
                            $work_hours = $service_product_item->work_hours;
                            $op=OrderProduct::find($order_product_item->order_product_id);
                            $product=ProductPackage::find($op->product_id);
                            $st=$product->classify;
                            $product_engineer_id=$op->engineer_id;
                            break;
                    }
                    if($product_engineer_id == 0){
                        $product_engineer = $order_engineer;
                    }else{
                        $product_engineer = MaintenanceEngineers::where('id',$product_engineer_id)->first();
                    }

                    $service_classification = Report::get_service_classification($st);
                    $_work_hour_report = array(
                        'order_id' => $_ro->order_id, //派工单号
                        'pay_time' => $_ro->pay_time,   //结算日期
                        'name' => $_ro->name,   //客户名称
                        'car_no' => $_ro->car_no,    //车牌号
                        'status' => $_ro->status,    //状态
                        'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                        'car_brand_name' => $_ro->car_brand_name,    //车辆品牌
                        'car_series_name' => $_ro->car_series_name,    //车辆系列
                        'car_model_name' => $_ro->car_model_name,    //车辆型号
                        'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                        'service_id' => $service_id,    //工时编码
                        'item_name' => $order_product_item->item_name,   //名称
                        'amount' => $order_product_item->amount,   //工时费
                        'discount' => 0,    //折扣
                        'real_amount' => $order_product_item->amount,  //实际费用
                        'counter_amount' => -$_roi->return_amount,  //减免实收
                        'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                        //班组
                        //工时类型
                        'level_1_id' => $service_classification['level_1_id'],
                        'level_1_name' => $service_classification['level_1_name'],
                        'level_2_id' => $service_classification['level_2_id'],
                        'level_2_name' => $service_classification['level_2_name'],
                        'level_3_id' => $service_classification['level_3_id'],
                        'level_3_name' => $service_classification['level_3_name'],
                        //是否收费
                        'work_type' => $work_type,    //工时定额
                        'work_hours' => $work_hours,    //工时
                        //接车员
                        //品牌
                    );
                    array_push($work_hour_report,$_work_hour_report);
                }
            }
        }
        //洗车单 退货
        $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->select(array('return_carwash_order.id',"car_wash_order.*"))
            ->get();
        foreach($wash_os_r as $wor){
            $pp = ProductPackage::find($wor->producd_id);
            $ppi=ProductPackItem::where("pack_id",$wor->producd_id)->where("status",1)->where("type",2)->first();
            $spi=ServiceProductItem::find($ppi->item_id);
            $sc = Report::get_service_classification($pp->classify);
            if(!is_null($wor->engineer_id)) {
                $order_engineer = MaintenanceEngineers::where('id', $wor->engineer_id)->first();
                if($order_engineer) {
                    $en_name = $order_engineer->name;
                }else{
                    $en_name = "";
                }
            }else{
                $en_name = "";
            }
            $_work_hour_report = array(
                'order_id' => $wor->order_id, //派工单号
                'pay_time' => $wor->pay_time,   //下单日期
                'name' => '',   //客户名称
                'car_no' => $wor->car_no,    //车牌号
                'status' => $wor->status,    //状态
                'engineer_id' => $en_name,   //订单技师
                'car_brand_name' => '',    //车辆品牌
                'car_series_name' => '',    //车辆系列
                'car_model_name' => '',    //车辆型号
                'vin_code' => '',  //VIN号
                'service_id' => $spi->id,    //工时编码
                'item_name' => $spi->name,   //名称
                'amount' => $spi->price,   //工时费
                'discount' => 1,    //折扣
                'real_amount' => $wor->pay_amount,  //实际费用
                'counter_amount' => -$wor->pay_amount,  //减免实收.

                'maintence_engineer_id' => $en_name,    //修理工
                //班组
                //工时类型
                'level_1_id' => $sc['level_1_id'],
                'level_1_name' => $sc['level_1_name'],
                'level_2_id' => $sc['level_2_id'],
                'level_2_name' => $sc['level_2_name'],
                'level_3_id' => $sc['level_3_id'],
                'level_3_name' => $sc['level_3_name'],
                //是否收费
                'work_type' => $spi->type,    //工时定额
                'work_hours' => $spi->work_hours,    //工时
                //接车员
                //品牌
            );
            array_push($work_hour_report,$_work_hour_report);
        }
        return $work_hour_report;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-07-19 15:47:09
     * @Description: 未结算的工时分类统计
     */
    public static function temp_get_work_hour_report($shop_id){
        $work_hour_report = array();
        $order = Orders::whereIn('status',array(1216,1416,1215))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            $user_car_info = UserCarInfo::where('id',$_order->user_car_id)->first();
            $order_product = OrderProduct::where('order_id',$_order->order_id)
                ->where('status',1)
                ->get();
            foreach ($order_product as $_order_product){
                if($_order_product->product_type=='enterprise'){//说明是 企业报
                    $opep=OrderProductEsPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        $item=OrderProductEsItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                        if(!is_null($item)){
                            foreach ($item as $_item){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->where('status',1)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;

                                $order_engineer = MaintenanceEngineers::where('id',$_order->engineer_id)->first();
                                if($_opep->engineer_id == 0){
                                    $product_engineer = $order_engineer;
                                }else{
                                    $product_engineer = MaintenanceEngineers::where('id',$_opep->engineer_id)->first();
                                }
                                $_work_hour_report = array(
                                    'order_id' => $_order->order_id, //派工单号
                                    'pay_time' => $_order->pay_time,   //结算日期
                                    'created_at' => $_order->created_at."",   //下单日期
                                    'name' => $_order->name,   //客户名称
                                    'car_no' => $_order->car_no,    //车牌号
                                    'status' => $_order->status,    //状态
                                    'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                    'car_brand_name' => $_order->car_brand_name,    //车辆品牌
                                    'car_series_name' => $_order->car_series_name,    //车辆系列
                                    'car_model_name' => $_order->car_model_name,    //车辆型号
                                    'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                    'service_id' => $service_id,    //工时编码
                                    'item_name' => $_item->item_name,   //名称
                                    'amount' => $_item->amount,   //工时费
                                    'discount' => 0,    //折扣
                                    'real_amount' => $_item->amount,  //实际费用
                                    'counter_amount' => $_item->counter_amount,  //减免实收
                                    'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                    //班组
                                    //工时类型
                                    'level_1_id' => $service_classification['level_1_id'],
                                    'level_1_name' => $service_classification['level_1_name'],
                                    'level_2_id' => $service_classification['level_2_id'],
                                    'level_2_name' => $service_classification['level_2_name'],
                                    'level_3_id' => $service_classification['level_3_id'],
                                    'level_3_name' => $service_classification['level_3_name'],
                                    //是否收费
                                    'work_type' => $work_type,    //工时定额
                                    'work_hours' => $work_hours,    //工时
                                    //接车员
                                    //品牌
                                );
                                array_push($work_hour_report,$_work_hour_report);
                            }
                        }
                    }
                }elseif($_order_product->product_type=='activity'){//说明是 活动包
                    $opep=OrderProductAcPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $service_detail = Report::get_service_detail($_opep->package_id, 'base');
                        $service_classification = Report::get_service_classification($service_detail->classify);
                        $item=OrderProductAcItem::where("order_product_package_id",$_opep->id)->where('type',2)->get();
                        if(!is_null($item)){
                            foreach ($item as $_item){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->where('status',1)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;

                                $order_engineer = MaintenanceEngineers::where('id',$_order->engineer_id)->first();
                                if($_opep->engineer_id == 0){
                                    $product_engineer = $order_engineer;
                                }else{
                                    $product_engineer = MaintenanceEngineers::where('id',$_opep->engineer_id)->first();
                                }
                                $_work_hour_report = array(
                                    'order_id' => $_order->order_id, //派工单号
                                    'created_at' => $_order->created_at."",   //下单日期
                                    'pay_time' => $_order->pay_time,   //结算日期
                                    'name' => $_order->name,   //客户名称
                                    'car_no' => $_order->car_no,    //车牌号
                                    'status' => $_order->status,    //状态
                                    'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                    'car_brand_name' => $_order->car_brand_name,    //车辆品牌
                                    'car_series_name' => $_order->car_series_name,    //车辆系列
                                    'car_model_name' => $_order->car_model_name,    //车辆型号
                                    'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                    'service_id' => $service_id,    //工时编码
                                    'item_name' => $_item->item_name,   //名称
                                    'amount' => $_item->amount,   //工时费
                                    'discount' => 0,    //折扣
                                    'real_amount' => $_item->amount,  //实际费用
                                    'counter_amount' => $_item->counter_amount,  //减免实收
                                    'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                    //班组
                                    //工时类型
                                    'level_1_id' => $service_classification['level_1_id'],
                                    'level_1_name' => $service_classification['level_1_name'],
                                    'level_2_id' => $service_classification['level_2_id'],
                                    'level_2_name' => $service_classification['level_2_name'],
                                    'level_3_id' => $service_classification['level_3_id'],
                                    'level_3_name' => $service_classification['level_3_name'],
                                    //是否收费
                                    'work_type' => $work_type,    //工时定额
                                    'work_hours' => $work_hours,    //工时
                                    //接车员
                                    //品牌
                                );
                                array_push($work_hour_report,$_work_hour_report);
                            }
                        }
                    }
                }else{
                    if($_order_product->product_type == 'base'){
                        $item = OrderProductBaseItem::where('order_product_package_id',$_order_product->id)
                            ->where('type',2)
                            ->get();
                    }elseif($_order_product->product_type == 'non_standard'){
                        $item = OrderProductNonStandard::where('order_product_id',$_order_product->id)
                            ->where('service_type',2)
                            ->where('status',1)
                            ->get();
                    }elseif($_order_product->product_type == 'service_card'){
                        $item = OrderProductScItem::where('order_product_id',$_order_product->id)
                            ->where('type',2)
                            ->get();
                    }else{
                        $item = null;
                        continue;
                    }
                    $service_detail = Report::get_service_detail($_order_product->product_id, $_order_product->product_type);
                    $service_classification = Report::get_service_classification($service_detail->classify);
                    if(!is_null($item)){
                        foreach ($item as $_item){
                            if($_order_product->product_type == 'base'){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->where('status',1)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;
                            }elseif($_order_product->product_type == 'non_standard'){
                                $service_id = 0;
                                $work_type = $_item->num;
                                $work_hours = $_item->amount;
                            }elseif($_order_product->product_type == 'service_card'){
                                $service_product_item = ServiceProductItem::where('id',$_item->item_id)->first();
                                $service_id = $service_product_item->id;
                                $work_type = $service_product_item->type;
                                $work_hours = $service_product_item->work_hours;
                            }else{
                                $service_id = 0;
                                $work_type = 0;
                                $work_hours = 0;
                            }
                            $order_engineer = MaintenanceEngineers::where('id',$_order->engineer_id)->where('status',1)->first();
                            if($_order_product->engineer_id == 0){
                                $product_engineer = $order_engineer;
                            }else{
                                $product_engineer = MaintenanceEngineers::where('id',$_order_product->engineer_id)->where('status',1)->first();
                            }
                            $_work_hour_report = array(
                                'order_id' => $_order->order_id, //派工单号
                                'created_at' => $_order->created_at."",   //下单日期
                                'name' => $_order->name,   //客户名称
                                'car_no' => $_order->car_no,    //车牌号
                                'status' => $_order->status,    //状态
                                'engineer_id' => isset($order_engineer->name)?$order_engineer->name:'',   //订单技师
                                'car_brand_name' => $_order->car_brand_name,    //车辆品牌
                                'car_series_name' => $_order->car_series_name,    //车辆系列
                                'car_model_name' => $_order->car_model_name,    //车辆型号
                                'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
                                'service_id' => $service_id,    //工时编码
                                'item_name' => $_item->item_name,   //名称
                                'amount' => $_item->amount,   //工时费
                                'discount' => $_item->discount,    //折扣
                                'real_amount' => $_item->amount * $_item->discount,  //实际费用
                                'maintence_engineer_id' => isset($product_engineer->name)?$product_engineer->name:'',    //修理工
                                //班组
                                //工时类型
                                'level_1_id' => $service_classification['level_1_id'],
                                'level_1_name' => $service_classification['level_1_name'],
                                'level_2_id' => $service_classification['level_2_id'],
                                'level_2_name' => $service_classification['level_2_name'],
                                'level_3_id' => $service_classification['level_3_id'],
                                'level_3_name' => $service_classification['level_3_name'],
                                //是否收费
                                'work_type' => $work_type,    //工时定额
                                'work_hours' => $work_hours,    //工时
                                //接车员
                                //品牌
                            );
                            array_push($work_hour_report,$_work_hour_report);
                        }
                    }
                }
            }
        }
        //洗车单
        $wash_os = CarWashOrder::where('status',1411)
            ->where('shop_id',$shop_id)
            ->get();
        foreach($wash_os as $wo){
            $pp = ProductPackage::find($wo->producd_id);
            $ppi=ProductPackItem::where("pack_id",$wo->producd_id)->where("status",1)->where("type",2)->first();
            $spi=ServiceProductItem::find($ppi->item_id);
            $sc = Report::get_service_classification($pp->classify);
            if(!is_null($wo->engineer_id)) {
                $order_engineer = MaintenanceEngineers::where('id', $wo->engineer_id)->where('status', 1)->first();
                if($order_engineer) {
                    $en_name = $order_engineer->name;
                }else{
                    $en_name = "";
                }
            }else{
                $en_name = "";
            }
            //判断显示折扣
            if($wo->pay_type == 49){//说明是 洗车卡 支付
                $dis_count = 0;
            }elseif($wo->pay_type == 3){
                $dis_count = 0.9;
            }else{
                $dis_count = 1;
            }
            $_work_hour_report = array(
                'data_type'=>'wash_order',
                'order_id' => $wo->order_id, //派工单号
                'created_at' => $wo->created_at . "",   //下单日期
                'name' => '',   //客户名称
                'car_no' => $wo->car_no,    //车牌号
                'status' => $wo->status,    //状态
                'engineer_id' => $en_name,   //订单技师
                'car_brand_name' => '',    //车辆品牌
                'car_series_name' => '',    //车辆系列
                'car_model_name' => '',    //车辆型号
                'vin_code' => '',  //VIN号
                'service_id' => $spi->id,    //工时编码
                'item_name' => $spi->name,   //名称
                'amount' => $spi->price,   //工时费
                'discount' => $dis_count,    //折扣
                'real_amount' => $wo->pay_amount,  //实际费用
                'maintence_engineer_id' => $en_name,    //修理工
                //班组
                //工时类型
                'level_1_id' => $sc['level_1_id'],
                'level_1_name' => $sc['level_1_name'],
                'level_2_id' => $sc['level_2_id'],
                'level_2_name' => $sc['level_2_name'],
                'level_3_id' => $sc['level_3_id'],
                'level_3_name' => $sc['level_3_name'],
                //是否收费
                'work_type' => $spi->type,    //工时定额
                'work_hours' => $spi->work_hours,    //工时
                //接车员
                //品牌
            );
            array_push($work_hour_report,$_work_hour_report);
        }
        return $work_hour_report;
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-06-01 22:00:53
     * @Description: 获取技师的业绩  废弃
     */
//    public static function get_enginss_report($begin_date,$end_date,$shop_id){
//        $work_hour_report = array();
//        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
//            ->whereBetween('pay_time',array($begin_date,$end_date))
//            ->where('shop_id',$shop_id)
//            ->where('place',2)
//            ->get();
//
//
//        $meengineera_array=array();
//        foreach ($order as $_order){
//            if(isset($meengineera_array[$_order->engineer_id])){//如果存在这个 技师的信息
//                $meengineera_array[$_order->engineer_id]["all_order"]=$meengineera_array[$_order->engineer_id]["all_order"]+1;//接车数量数量+1
//                $meengineera_array[$_order->engineer_id]["check_car"]=$meengineera_array[$_order->engineer_id]["check_car"]+1;//检查车数量
//            }else{
//                $meengineera_array[$_order->engineer_id]["check_car"]=array(
//                    "all_order"=>1,
//                    "check_car"=>1,
//                    "all_product_amount"=>0,
//                    "product"=>array(),
//                );
//            }
//
//            $order_product = OrderProduct::where('order_id',$_order->order_id)
//                ->where('status',1)
//                ->get();
//
//            foreach ($order_product as $_op) {
//                if($_op->engineer_id==0&&$_me->id==$_order->engineer_id){//说明 该服务是该技师完成
//                    isset($_me->all_product_amount)?($_me->all_product_amount=$_me->all_product_amount+$_op->amount-$_op->discount):($_me->all_product_amount=$_op->amount-$_op->discount);//完成的 项目流水
//                }else if($_op->engineer_id==$_me->id){//s说明是别的技师做的服务
//                    isset($_me->all_product_amount)?($_me->all_product_amount=$_me->all_product_amount+$_op->amount-$_op->discount):($_me->all_product_amount=$_op->amount-$_op->discount);//完成的 项目流水
//                }
//            }
//
//
//
//
//
//            foreach ($order_product as $_order_product){
//                if($_order_product->product_type == 'base'){
//                    $item = OrderProductBaseItem::where('order_product_package_id',$_order_product->id)
//                        ->where('type',2)
//                        ->get();
//                }elseif($_order_product->product_type == 'non_standard'){
//                    $item = OrderProductNonStandard::where('order_product_id',$_order_product->id)
//                        ->where('service_type',2)
//                        ->get();
//                }else{
//                    $item = null;
//                }
//                if(!is_null($item)){
//                    foreach ($item as $_item){
//                        if($_order_product->product_type == 'base'){
//                            $service_product_item = ServiceProductItem::where('id',$_item->item_id)->where('status',1)->first();
//                            $service_id = $service_product_item->id;
//                            $work_type = $service_product_item->type;
//                            $work_hours = $service_product_item->work_hours;
//                        }elseif($_order_product->product_type == 'non_standard'){
//                            $service_id = 0;
//                            $work_type = $_item->num;
//                            $work_hours = $_item->amount;
//                        }else{
//                            $service_id = 0;
//                            $work_type = 0;
//                            $work_hours = 0;
//                        }
//                        $_work_hour_report = array(
//                            'order_id' => $_order->order_id, //派工单号
//                            'pay_time' => $_order->pay_time,   //结算日期
//                            'name' => $_order->name,   //客户名称
//                            'car_no' => $_order->car_no,    //车牌号
//                            'status' => $_order->status,    //状态
//                            'engineer_id' => $_order->engineer_id,   //订单技师
//                            'car_brand_name' => $_order->car_brand_name,    //车辆品牌
//                            'car_series_name' => $_order->car_series_name,    //车辆系列
//                            'car_model_name' => $_order->car_model_name,    //车辆型号
//                            'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'',  //VIN号
//                            'service_id' => $service_id,    //工时编码
//                            'item_name' => $_item->item_name,   //名称
//                            'amount' => $_item->amount,   //工时费
//                            'discount' => $_item->discount,    //折扣
//                            'real_amount' => $_item->amount * $_item->discount,  //实际费用
//                            'maintence_engineer_id' => $_order->engineer_id,    //修理工
//                                //班组
//                                //工时类型
//                                //三级分类
//                                //是否收费
//                            'work_type' => $work_type,    //工时定额
//                            'work_hours' => $work_hours,    //工时
//                                //接车员
//                                //品牌
//                        );
//                    }
//                }
//            }
//            array_push($work_hour_report,$_work_hour_report);
//        }
//        return $work_hour_report;
//    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-06-01 21:10:23
     * @Description: 结算的产品包的统计
     */
    public static function get_work_hour_product($begin_date,$end_date,$shop_id){

        $work_hour_report = array();
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            $order_product = OrderProduct::where('order_id',$_order->order_id)
                ->where('status',1)
                ->get();
            foreach ($order_product as $_order_product){
                if($_order_product->product_type=='enterprise'){//说明是 企业报
                    $opep=OrderProductEsPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $supply_amount=0;
                        $service_amount=0;
                        $propack=ProductPackage::find($_opep->package_id);
                        //获取标准的工时成本
                        $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_esitem` where order_product_package_id=".$_opep->id." and type=2");
                        $service_amount=$count[0]->amount;
                        //获取耗材成本
                        $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='enterprise' and product_item_id in(SELECT id FROM `order_product_esitem` where order_product_package_id=".$_opep->id." and type=1)");
                        foreach ($mot_ids as $_mot) {
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                            if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                continue;
                            }
                            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();

                            $temp_amount=0;
                            foreach ($before_return_ids as $bri) {
                                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                $temp_amount+= $return_amount[0]->amount;
                            }


                            $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                            $supply_amount+=($supply_count[0]->amount+$temp_amount);
                        }
                        if(!is_null($propack)){
                            $service_classification = Report::get_service_classification($propack->classify);
                            $_work_hour_report = array(
                                'order_id' => $_order->order_id, //派工单号
                                'product_name' => $_order_product->product_name.' '.$propack->name,   //客户名称
                                'product_id' => $propack->id,   //客户名称
                                'product_type' =>0,   //服务包是标准还是非标  0是标准 1是非标
                                'product_amount' => $_opep->amount,
                                'product_discount' => 0,
                                'counter_amount' => $_opep->counter_amount,
                                'supply_amount' => $supply_amount,
                                'service_amount' => $service_amount,
                                'level_1_id' => $service_classification['level_1_id'],
                                'level_1_name' => $service_classification['level_1_name'],
                                'level_2_id' => $service_classification['level_2_id'],
                                'level_2_name' => $service_classification['level_2_name'],
                                'level_3_id' => $service_classification['level_3_id'],
                                'level_3_name' => $service_classification['level_3_name'],
                                'reteurn_type' => 0,
                            );
                            array_push($work_hour_report,$_work_hour_report);
                        }
                    }
                }elseif($_order_product->product_type=='activity'){//说明是 活动包
                    $opep=OrderProductAcPackage::where("order_product_id",$_order_product->id)->get();
                    foreach ($opep as $_opep){
                        $supply_amount=0;
                        $service_amount=0;
                        $propack=ProductPackage::find($_opep->package_id);
                        //获取标准的工时成本
                        $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_acitem` where order_product_package_id=".$_opep->id." and type=2");
                        $service_amount=$count[0]->amount;
                        //获取耗材成本
                        $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='activity' and product_item_id in(SELECT id FROM `order_product_acitem` where order_product_package_id=".$_opep->id." and type=1)");
                        foreach ($mot_ids as $_mot) {
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                            if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                continue;
                            }
                            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();

                            $temp_amount=0;
                            foreach ($before_return_ids as $bri) {
                                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                $temp_amount+= $return_amount[0]->amount;
                            }


                            $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                            $supply_amount+=($supply_count[0]->amount+$temp_amount);
                        }
                        if(!is_null($propack)){
                            $service_classification = Report::get_service_classification($propack->classify);
                            $_work_hour_report = array(
                                'order_id' => $_order->order_id, //派工单号
                                'product_name' => $_order_product->product_name.' '.$propack->name,   //客户名称
                                'product_id' => $propack->id,   //客户名称
                                'product_type' =>0,   //服务包是标准还是非标  0是标准 1是非标
                                'product_amount' => $_opep->amount,
                                'product_discount' => 0,
                                'counter_amount' => $_opep->counter_amount,
                                'supply_amount' => $supply_amount,
                                'service_amount' => $service_amount,
                                'level_1_id' => $service_classification['level_1_id'],
                                'level_1_name' => $service_classification['level_1_name'],
                                'level_2_id' => $service_classification['level_2_id'],
                                'level_2_name' => $service_classification['level_2_name'],
                                'level_3_id' => $service_classification['level_3_id'],
                                'level_3_name' => $service_classification['level_3_name'],
                                'reteurn_type' => 0,
                            );
                            array_push($work_hour_report,$_work_hour_report);
                        }
                    }
                }else{
                    $supply_amount=0;
                    $service_amount=0;
                    if($_order_product->product_type == 'base'){
                        $propack=ProductPackage::find($_order_product->product_id);
                        //获取标准的工时成本
                        $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_base_item` where order_product_package_id=".$_order_product->id." and type=2");
                        $service_amount=$count[0]->amount;
                        //获取耗材成本
                        $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='base' and product_item_id in(SELECT id FROM `order_product_base_item` where order_product_package_id=".$_order_product->id." and type=1)");
                        foreach ($mot_ids as $_mot) {
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                            if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                continue;
                            }
                            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                            $temp_amount=0;
                            foreach ($before_return_ids as $bri) {
                                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                $temp_amount+= $return_amount[0]->amount;
                            }


                            $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                            $supply_amount+=($supply_count[0]->amount+$temp_amount);
                        }
                    }elseif($_order_product->product_type == 'non_standard'){
                        $propack=Service::find($_order_product->product_id);
                        //获取非标的工时成本
                        $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_non_standard` where order_product_id=".$_order_product->id." and service_type=2 and status=1");
                        $service_amount=$count[0]->amount;
                        //获取耗材成本
                        $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='non_standard' and product_item_id in(SELECT id FROM `order_product_non_standard` where order_product_id=".$_order_product->id." and service_type=1)");
                        foreach ($mot_ids as $_mot) {
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                            if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                continue;
                            }
                            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                            $temp_amount=0;
                            foreach ($before_return_ids as $bri) {
                                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                $temp_amount+= $return_amount[0]->amount;
                            }

                            $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                            $supply_amount+=($supply_count[0]->amount+$temp_amount);
                        }
                    }elseif($_order_product->product_type == 'service_card'){
                        $propack=ProductPackage::find($_order_product->product_id);
                        //获取标准的工时成本
                        $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_sc_item` where order_product_id=".$_order_product->id." and type=2");
                        $service_amount=$count[0]->amount;
                        //获取耗材成本
                        $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='service_card' and product_item_id in(SELECT id FROM `order_product_sc_item` where order_product_id=".$_order_product->id." and type=1)");
                        foreach ($mot_ids as $_mot) {
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                            if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                                continue;
                            }
                            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                            $temp_amount=0;
                            foreach ($before_return_ids as $bri) {
                                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                                $temp_amount+= $return_amount[0]->amount;
                            }


                            $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                            $supply_amount+=($supply_count[0]->amount+$temp_amount);
                        }
                    }else{
                        $propack = null;
                        continue;
                    }

                    if(!is_null($propack)){
                        $service_classification = Report::get_service_classification($propack->classify);
                        $_work_hour_report = array(
                            'order_id' => $_order->order_id, //派工单号
                            'product_name' => $propack->name,   //客户名称
                            'product_id' => $propack->id,   //客户名称
                            'product_type' => $_order_product->product_type == 'non_standard'?1:0,   //服务包是标准还是非标  0是标准 1是非标
                            'product_amount' => $_order_product->amount,
                            'product_discount' => $_order_product->discount,
                            'counter_amount' => $_order_product->counter_amount,
                            'supply_amount' => $supply_amount,
                            'service_amount' => $service_amount,
                            'level_1_id' => $service_classification['level_1_id'],
                            'level_1_name' => $service_classification['level_1_name'],
                            'level_2_id' => $service_classification['level_2_id'],
                            'level_2_name' => $service_classification['level_2_name'],
                            'level_3_id' => $service_classification['level_3_id'],
                            'level_3_name' => $service_classification['level_3_name'],
                            'reteurn_type' => 0,
                        );
                        array_push($work_hour_report,$_work_hour_report);
                    }
                }
            }
        }
        //洗车单
        $wash_os = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->get();

        foreach($wash_os as $wo){
            $pp = ProductPackage::find($wo->producd_id);
            $sc = Report::get_service_classification($pp->classify);
            $_work_hour_report_1 = array(
                'order_id' => $wo->order_id, //派工单号
                'product_name' => $pp->name,   //客户名称
                'product_id' => $wo->producd_id,   //客户名称
                'product_type' => 0, //  0是标准 1是非标
                'product_amount' => $wo->amount,
                'product_discount' => $wo->dis_amount,
                'counter_amount' => $wo->pay_amount,
                'supply_amount' => 0,
                'service_amount' => $wo->amount,
                'level_1_id' => $sc['level_1_id'],
                'level_1_name' => $sc['level_1_name'],
                'level_2_id' => $sc['level_2_id'],
                'level_2_name' => $sc['level_2_name'],
                'level_3_id' => $sc['level_3_id'],
                'level_3_name' => $sc['level_3_name'],
                'reteurn_type' => 0,
            );
            array_push($work_hour_report,$_work_hour_report_1);
        }
        //计算这个时间段内 退货的 记录

        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id',"orders.*"))->get();
        foreach ($ro as $_ro){
            $roi=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->where("type",3)->first();//说明是 均摊的退货
            if($roi){
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",3)->where("type",3)->get();
            }else{
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->get();//
            }
            $pacek_arr=array();
            foreach ($ori as $_roi) {
                if($_roi->type != 4) {
                    switch ($_roi->product_type) {
                        case 'enterprise':
                            $order_product_item = OrderProductEsItem::find($_roi->order_product_item_id);
                            $op = OrderProductEsPackage::find($order_product_item->order_product_package_id);
                            $product = ProductPackage::find($op->package_id);
                            $product_name = $op->package_name;
                            $item_type = $order_product_item->type;
                            $st = $product->classify;
                            break;
                        case 'activity':
                            $order_product_item = OrderProductAcItem::find($_roi->order_product_item_id);
                            $op = OrderProductAcPackage::find($order_product_item->order_product_package_id);
                            $product = ProductPackage::find($op->package_id);
                            $product_name = $op->package_name;
                            $item_type = $order_product_item->type;
                            $st = $product->classify;
                            break;
                        case 'promotion':
                            break;
                        case 'base':
                            $order_product_item = OrderProductBaseItem::find($_roi->order_product_item_id);
                            $op = OrderProduct::find($order_product_item->order_product_package_id);
                            $product = ProductPackage::find($op->product_id);
                            $product_name = $op->package_name;
                            $item_type = $order_product_item->type;
                            $st = $product->classify;
                            break;
                        case 'non_standard':
                            $order_product_item = OrderProductNonStandard::find($_roi->order_product_item_id);
                            $op = OrderProduct::find($order_product_item->order_product_id);
                            $product_name = $op->package_name;
                            $item_type = $order_product_item->service_type;
                            $product = Service::find($op->product_id);
                            $st = $product->classify;
                            break;
                        case 'service_card':
                            $order_product_item = OrderProductScItem::find($_roi->order_product_item_id);
                            $op = OrderProduct::find($order_product_item->order_product_id);
                            $product = ProductPackage::find($op->product_id);
                            $product_name = $op->package_name;
                            $item_type = $order_product_item->type;
                            $st = $product->classify;
                            break;
                    }
                    $counter_amount=$supply_amount=$service_amount=0;
                    $counter_amount=-$_roi->return_amount;
                    switch ($_roi->type) {
                        case '0'://（退库存）退成本退钱
                            $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("product_type",$_roi->product_type)->where("product_item_id",$_roi->order_product_item_id)->get();

                            foreach ($before_return as $_br) {
                                $temp_msi=MaintenanceStorageItem::where("return_order_id",$_br->id)->where("status",1)->get();
                                foreach ($temp_msi as $_ts) {
                                    $temp_moi=MaintenanceOutItem::find($_ts->out_id);
                                    $supply_amount=$supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                }
                            }
                            $supply_amount=-$supply_amount;
                            break;
                        case '1'://1配件
                            break;
                        case '2'://2工时
                            $service_amount=-$_roi->return_amount;
                            break;
                        case '3'://3均摊
                            if($item_type==2)//说明是工时
                                $service_amount=-$_roi->return_amount;
                            break;
                    }
                    if(isset($pacek_arr[$_roi->product_type."_".$op->id])){
                        $temp=$pacek_arr[$_roi->product_type."_".$op->id];
                        $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
                        $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
                        $temp["service_amount"]=$temp["service_amount"]+$service_amount;
                        $pacek_arr[$_roi->product_type."_".$op->id]=$temp;
                    }else{
                        $pacek_arr[$_roi->product_type."_".$op->id]=array('product_name'=>$product_name,'product_type'=>$_roi->product_type=="non_standard"?1:0,'product_id'=>$product->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
                    }
                }else {
                    $ops = OrderProduct::find($_roi->order_product_id);
                    if ($ops->product_type == 'enterprise') {//说明是 企业报
                        $opep = OrderProductEsPackage::where("order_product_id", $_roi->order_product_id)->get();
                        foreach ($opep as $_opep) {
                            $product = ProductPackage::find($_opep->package_id);
                            $product_name = $_opep->package_name;
                            $st = $product->classify;
                            $item = OrderProductEsItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
                            foreach($item as $_it){
                                $item_type = $_it->type;
                                $counter_amount=$supply_amount=$service_amount=0;                                
                                if($item_type == 1) {
                                    $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                    foreach ($before_return as $_br) {
                                        $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                        foreach ($temp_msi as $_ts) {
                                            $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                            $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                        }
                                    }
                                    $supply_amount = -$supply_amount;
                                    $counter_amount=-$_it->counter_amount*$_it->num;
                                }else {
                                    $service_amount = -$_it->counter_amount;
                                    $counter_amount=-$_it->counter_amount;
                                }
                                if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
                                    $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
                                    $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
                                    $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
                                    $temp["service_amount"]=$temp["service_amount"]+$service_amount;
                                    $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
                                }else{
                                    $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_type'=>0,'product_id'=>$product->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
                                }
                            }
                        }
                    } elseif ($ops->product_type == 'activity') {//说明是 企业报
                        $opep = OrderProductAcPackage::where("order_product_id", $_roi->order_product_id)->get();
                        foreach ($opep as $_opep) {
                            $product = ProductPackage::find($_opep->package_id);
                            $product_name = $_opep->package_name;
                            $st = $product->classify;
                            $item = OrderProductAcItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
                            foreach($item as $_it){
                                $item_type = $_it->type;
                                $counter_amount=$supply_amount=$service_amount=0;                                
                                if($item_type == 1) {
                                    $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                    foreach ($before_return as $_br) {
                                        $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                        foreach ($temp_msi as $_ts) {
                                            $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                            $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                        }
                                    }
                                    $supply_amount = -$supply_amount;
                                    $counter_amount=-$_it->counter_amount*$_it->num;
                                }else {
                                    $service_amount = -$_it->counter_amount;
                                    $counter_amount=-$_it->counter_amount;
                                }
                                if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
                                    $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
                                    $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
                                    $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
                                    $temp["service_amount"]=$temp["service_amount"]+$service_amount;
                                    $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
                                }else{
                                    $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_type'=>0,'product_id'=>$product->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
                                }
                            }
                        }
                    } elseif ($ops->product_type == 'base') {//说明是 基础
                        $item = OrderProductBaseItem::where("order_product_package_id", $ops->id)->get();
                        $product = ProductPackage::find($ops->product_id);
                        $product_name = $ops->product_name;
                        $st = $product->classify;
                        foreach($item as $_it){
                            $item_type = $_it->type;
                            $counter_amount=$supply_amount=$service_amount=0;                            
                            if($item_type == 1) {
                                $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                foreach ($before_return as $_br) {
                                    $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                    foreach ($temp_msi as $_ts) {
                                        $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                        $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                    }
                                }
                                $supply_amount = -$supply_amount;
                                $counter_amount=-$_it->counter_amount*$_it->num;
                            }else {
                                $service_amount = -$_it->counter_amount;
                                $counter_amount=-$_it->counter_amount;
                            }
                            if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
                                $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
                                $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
                                $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
                                $temp["service_amount"]=$temp["service_amount"]+$service_amount;
                                $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
                            }else{
                                $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_type'=>0,'product_id'=>$product->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
                            }
                        }
                    } elseif ($ops->product_type == 'non_standard') {//说明是 非标
                        $item = OrderProductNonStandard::where("order_product_id", $ops->id)->get();
                        $product_name = $ops->product_name;
                        $s = Service::find($ops->product_id);
                        $st = $s->classify;
                        foreach($item as $_it){
                            $item_type = $_it->service_type;
                            $counter_amount=$supply_amount=$service_amount=0;                            
                            if($item_type == 1) {
                                $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                foreach ($before_return as $_br) {
                                    $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                    foreach ($temp_msi as $_ts) {
                                        $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                        $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                    }
                                }
                                $supply_amount = -$supply_amount;
                                $counter_amount=-$_it->counter_amount*$_it->num;
                            }else {
                                $service_amount = -$_it->counter_amount;
                                $counter_amount=-$_it->counter_amount;
                            }                            
                            if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
                                $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
                                $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
                                $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
                                $temp["service_amount"]=$temp["service_amount"]+$service_amount;
                                $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
                            }else{
                                $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_type'=>1,'product_id'=>$s->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
                            }
                        }
                    } elseif ($ops->product_type == 'service_card') {//说明是 套餐卡
                        $item = OrderProductScItem::where("order_product_id", $ops->id)->get();
                        $product = ProductPackage::find($ops->product_id);
                        $product_name = $ops->product_name;
                        $st = $product->classify;
                        foreach($item as $_it){
                            $item_type = $_it->type;
                            $counter_amount=$supply_amount=$service_amount=0;                            
                            if($item_type == 1) {
                                $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_it->id)->get();
                                foreach ($before_return as $_br) {
                                    $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                    foreach ($temp_msi as $_ts) {
                                        $temp_moi = MaintenanceOutItem::find($_ts->out_id);
                                        $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
                                    }
                                }
                                $supply_amount = -$supply_amount;
                                $counter_amount=-$_it->counter_amount*$_it->num;
                            }else {
                                $service_amount = -$_it->counter_amount;
                                $counter_amount=-$_it->counter_amount;
                            }
                            if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
                                $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
                                $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
                                $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
                                $temp["service_amount"]=$temp["service_amount"]+$service_amount;
                                $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
                            }else{
                                $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_type'=>0,'product_id'=>$product->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
                            }
                        }
                    }
                }
            }
            foreach ($pacek_arr as $_pa) {
                $service_classification = Report::get_service_classification($_pa["st"]);
                $_work_hour_report = array(
                    'order_id' => $_ro->order_id, //派工单号
                    'product_name' => $_pa['product_name'],   //客户名称
                    'product_id' => $_pa['product_id'],   //客户名称
                    'product_type' =>$_pa['product_type'],   //服务包是标准还是非标  0是标准 1是非标
                    'product_amount' => 0,
                    'product_discount' => 0,
                    'counter_amount' => $_pa['counter_amount'],
                    'supply_amount' => $_pa['supply_amount'],
                    'service_amount' => $_pa['service_amount'],
                    'level_1_id' => $service_classification['level_1_id'],
                    'level_1_name' => $service_classification['level_1_name'],
                    'level_2_id' => $service_classification['level_2_id'],
                    'level_2_name' => $service_classification['level_2_name'],
                    'level_3_id' => $service_classification['level_3_id'],
                    'level_3_name' => $service_classification['level_3_name'],
                    'reteurn_type' => 1,
                );
                array_push($work_hour_report,$_work_hour_report);
            }
        }
        //洗车单 退货
        $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->select(array('return_carwash_order.id',"car_wash_order.*"))
            ->get();

        foreach($wash_os_r as $wor){
            $pp = ProductPackage::find($wor->producd_id);
            $sc = Report::get_service_classification($pp->classify);
            $_work_hour_report_2 = array(
                'order_id' => $wor->order_id, //派工单号
                'product_name' => $pp->name,   //客户名称
                'product_id' => $wor->producd_id,   //客户名称
                'product_type' => 0, //  0是标准 1是非标
                'product_amount' => 0,
                'product_discount' => 0,
                'counter_amount' => -$wor->pay_amount,
                'supply_amount' => 0,
                'service_amount' => -$wor->amount,
                'level_1_id' => $sc['level_1_id'],
                'level_1_name' => $sc['level_1_name'],
                'level_2_id' => $sc['level_2_id'],
                'level_2_name' => $sc['level_2_name'],
                'level_3_id' => $sc['level_3_id'],
                'level_3_name' => $sc['level_3_name'],
                'reteurn_type' => 1,
            );
            array_push($work_hour_report,$_work_hour_report_2);
        }
        //更换时间
        $begin_date=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));
        $out_list=DB::select("SELECT  moi.*
            FROM
                maintenance_out_item moi
            LEFT JOIN maintenance_out_list ON moi.list_id = maintenance_out_list.id
            WHERE
                maintenance_out_list.implement_time BETWEEN ?
            AND ?
            AND `maintenance_out_list`.`shop_id` = ?
            AND `maintenance_out_list`.`status` = 1
            AND `moi`.`status` = 1
            AND `maintenance_out_list`.`methed` = 3
            ORDER BY
                `maintenance_out_list`.`implement_time` ASC",array($begin_date,$end_date,$shop_id));
        //处理这个时段钣金喷漆的
        //暂时不用
        foreach ($out_list as $_pa) {
            $_work_hour_report = array(
                'order_id' => "", //派工单号
                'product_name' => "钣金喷漆",   //客户名称
                'product_id' => 0,   //客户名称
                'product_type' =>0,   //服务包是标准还是非标  0是标准 1是非标
                'product_amount' => 0,
                'product_discount' => 0,
                'counter_amount' =>0,
                'supply_amount' => $_pa->tax_purchase_price*($_pa->number-$_pa->return_numer),
                'service_amount' => 0,
                'level_1_id' => -1,
                'level_1_name' => "钣金喷漆",
                'level_2_id' => 0,
                'level_2_name' => 0,
                'level_3_id' => 0,
                'level_3_name' => 0,
                'reteurn_type' => 0,
            );
            array_push($work_hour_report,$_work_hour_report);
        }
        return $work_hour_report;
    }

    //进销存报表商品辅料库
    public static function get_storage_out_detail($begin_date,$end_date,$shop_id,$data_id){
        $storage_detail = array();
        $maintenance_storage_item = MaintenanceStorageItem::where('status',1)
            ->where('shop_id',$shop_id)
            ->whereBetween('updated_at',array($begin_date,$end_date))
            ->where('data_id',$data_id)
            ->get();
        foreach ($maintenance_storage_item as $_maintenance_storage_item){
            $item_detail = Report::get_supply_detail($_maintenance_storage_item->item_id,$_maintenance_storage_item->item_type);

        }

        return $storage_detail;
    }

    //进销存报表商品配件库
    //进销存报表已结辅料库
    //进销存报表已结配件库

    //毛利率分析
    public static function get_profit_report($begin_date,$end_date,$shop_id){
        $profit_report = array();
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->orderBy('pay_time',"asc")
            ->get();
        $as_amount=DB::select("SELECT sum((moi.number-moi.return_numer)*moi.tax_purchase_price) as amount FROM maintenance_out_list mol LEFT JOIN maintenance_out_item moi on mol.id=moi.list_id where mol.shop_id=1 and mol.`status`=1 and mol.out_type=1 and mol.methed=3 and mol.implement_time BETWEEN ? and ?",array($begin_date,$end_date));
        //查询分类
        $temp_filit=DB::select("SELECT sc3.* FROM service_classification sc3 LEFT JOIN service_classification sc2 on sc3.parent_id=sc2.id LEFT JOIN service_classification sc on sc2.parent_id=sc.id where sc3.`status`=1 and sc.id in(13,14,1553)");
        $filit=array();
        foreach ($temp_filit as $_tf) {
            $filit[]=$_tf->id;
        }

        $as_amount=$as_amount[0]->amount;
        $all_amount=0;
        foreach ($order as $_order) {
            //对该订单进行 钣金喷漆判断 如果存在 均摊钣金喷漆领料成本
            $procut_cs=DB::select("SELECT  order_product.product_type,service.classify sc,product_package.classify pc FROM  order_product LEFT JOIN service ON service.id = order_product.product_id LEFT JOIN product_package ON product_package.id = order_product.product_id WHERE   order_product.order_id =? AND order_product. STATUS = 1 AND order_product.product_type IN ('base','non_standard')",array($_order->order_id));
            $has_banji=0;
            foreach ($procut_cs as $_pc) {
                $classify=$_pc->product_type=="base"?$_pc->pc:$_pc->sc;
                if(in_array($classify, $filit)){//如果该订存在钣金喷漆项目
                    $all_amount+=$_order->payable_amount;
                    $has_banji=1;
                    break;
                }
            }

            $user_car_info = UserCarInfo::where('id',$_order->user_car_id)->first();
            $user = Users::where('id',$_order->user_id)->first();
            $order_engineer = MaintenanceEngineers::where('id',$_order->engineer_id)->where('status',1)->first();
            if(is_null($order_engineer)){
                $order_engineer_name = "上门车";
            }else{
                $order_engineer_name = $order_engineer->name;
            }
            $maintenance_out_order = MaintenanceOutOrder::where('order_id',$_order->order_id)
                ->whereIn('status',array(2,3))
                ->get();
            $amount = 0;
            foreach ($maintenance_out_order as $_maintenance_out_order){
                //结算后退货的数量
                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->sum("number");
                if($before_return==0&&$_maintenance_out_order->status==3){//如果是 这个出库申请是结算前 退货完成的  不统计
                    continue;
                }

                $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->select("id")->get();

                $temp_amount=0;
                foreach ($before_return_ids as $bri) {
                    $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                    $temp_amount+= $return_amount[0]->amount;
                }

                $maintenance_out_item = MaintenanceOutItem::where('order_out_id',$_maintenance_out_order->id)->get();
                foreach ($maintenance_out_item as $_maintenance_out_item){
                    $item_amount = ($_maintenance_out_item->number - $_maintenance_out_item->return_numer) * $_maintenance_out_item->tax_purchase_price;
                    $amount = $amount + $item_amount;
                }
                $amount=$amount+$temp_amount;
            }
            $remark=DB::select("SELECT GROUP_CONCAT(remark) as remank FROM order_remark where user_type='sc' and order_id=".$_order->order_id);
            $_profit_report = array(
                'data_type' => "normal_type",
                'order_id' => $_order->order_id, //派工单号
                'return_order_id' => "", //派工单号
                'car_no' => $_order->car_no, //车牌号
                'name' => $_order->name, //客户
                'car_model' => $_order->car_model_name, //车型
                'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'', //车架号
                'order_amount' => $_order->payable_amount, //维修收入
                'item_amount' => $amount, //配件成本
                'profit' => $_order->payable_amount - $amount, //单车毛利
                'profit_rate' => $_order->payable_amount==0?0:(($_order->payable_amount - $amount)/$_order->payable_amount) , //毛利率
                'phone' => $_order->phone, //电话
                'user_type' => $user->type, //车辆类型
                'mileage' => isset($user_car_info)?$user_car_info->mileage:'/', //行驶里程
                'engineer_id' => $order_engineer_name, //修理工
                'pay_time' => $_order->pay_time, //结算日期
                'order_type' => $_order->order_type, //订单类型
                'insurance' => isset($user_car_info)?$user_car_info->insurance:'', //保险到期日
                "remark"=>$remark[0]->remank,
                'has_banji'=>$has_banji,//包含不包含钣金喷漆
                'banjin_supply_amount'=>0,
                //客户单位
                //油漆辅料成本
                //维修类型
                //保险到期年
                //保险到期月
                //保险到期日
            );
            array_push($profit_report,$_profit_report);
        }
        //循环均摊钣金喷漆成本
        foreach ($profit_report as &$_t_p) {
            if($_t_p['has_banji']==1){
                if($all_amount==0){
                    $_t_p["banjin_supply_amount"]=0;
                }else{
                    $_t_p["banjin_supply_amount"]=($_t_p["order_amount"]/$all_amount)*$as_amount;
                }                   
            }
        }

        //洗车单
        $wash_os = CarWashOrder::leftJoin('users', 'car_wash_order.user_id', '=', 'users.id')->where('car_wash_order.status',1417)
            ->whereBetween('car_wash_order.pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)->select(array(
                'car_wash_order.order_id',
                'car_wash_order.car_no',
                'car_wash_order.pay_amount',
                'car_wash_order.pay_time',
                'users.real_name',
                'users.type'
            ))
            ->get();

        foreach($wash_os as $wo){
            if(!is_null($wo->engineer_id)) {
                $order_engineer = MaintenanceEngineers::where('id', $wo->engineer_id)->where('status', 1)->first();
                if($order_engineer) {
                    $en_name = $order_engineer->name;
                }else{
                    $en_name = "";
                }
            }else{
                $en_name = "";
            }
            $_work_hour_report_1 = array(
                'data_type' => 'wash_order',
                'order_id' => $wo->order_id, //派工单号
                'return_order_id' => "", //派工单号
                'car_no' => $wo->car_no, //车牌号
                'name' => $wo->real_name,   //客户名称
                'car_model' => '',   //
                'vin_code' => '',   //
                'order_amount' => $wo->pay_amount,   //
                'item_amount' => 0, //配件成本
                'profit' => $wo->pay_amount, //单车毛利
                'profit_rate' => 1, //单车毛利率
                'phone' => '', //电话
                'user_type' => $wo->type, //车辆类型
                'mileage' => '',
                'engineer_id' => $en_name, //修理工
                'pay_time' => $wo->pay_time, //结算日期
                'order_type' => 3, //订单类型
                'insurance' => '', //保险到期日
                "remark"=>'',
                'has_banji'=>0,//包含不包含钣金喷漆
                'banjin_supply_amount'=>0,
            );
            array_push($profit_report,$_work_hour_report_1);
        }

        //套餐卡单
        $sc_order = ServiceCardOrder::leftJoin('users', 'service_card_order.user_id', '=', 'users.id')
            ->whereIn('service_card_order.status',array(1018,1017,1217,1317,1417))
            ->whereBetween('service_card_order.pay_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->select(array(
                'service_card_order.order_id',
                'users.phone',
                'service_card_order.car_no',
                'users.real_name',
                'users.type',
                'service_card_order.pay_time',
                'service_card_order.amount',
               // 'orders.discount_amount',
                'service_card_order.pay_amount',
               // 'orders.order_type',
                'service_card_order.pay_type',
                'service_card_order.pay_time',
                'service_card_order.status'
            ))->orderBy('service_card_order.pay_time',"asc")
            ->get();

        foreach($sc_order as $sc){
            $_work_hour_report_1 = array(
                'data_type' => 'sc_order',
                'order_id' => $sc->order_id, //派工单号
                'return_order_id' => "", //派工单号
                'car_no' => $sc->car_no, //车牌号
                'name' => $sc->real_name,   //客户名称
                'car_model' => '',   //
                'vin_code' => '',   //
                'order_amount' => $sc->pay_amount,   //
                'item_amount' => 0, //配件成本
                'profit' => $sc->pay_amount, //单车毛利
                'profit_rate' => 1, //单车毛利率
                'phone' => $sc->phone, //电话
                'user_type' => $sc->type, //类型
                'mileage' => '',
                'engineer_id' => '', //修理工
                'pay_time' => $sc->pay_time, //结算日期
                'order_type' => 4, //订单类型
                'insurance' => '', //保险到期日
                "remark"=>'',
                'has_banji'=>0,//包含不包含钣金喷漆
                'banjin_supply_amount'=>0,
            );
            array_push($profit_report,$_work_hour_report_1);
        }

        //获取这段时间退货的
        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id',"orders.*"))->orderBy('return_order.implement_time',"asc")->get();
        foreach ($ro as $_ro) {
            $temp_ro=ReturnOrder::find($_ro->id);
            $user_car_info = UserCarInfo::where('id',$_ro->user_car_id)->first();
            $user = Users::where('id',$_ro->user_id)->first();
            $order_engineer = MaintenanceEngineers::where('id',$_ro->engineer_id)->where('status',1)->first();
            if(is_null($order_engineer)){
                $order_engineer_name = "上门车";
            }else{
                $order_engineer_name = $order_engineer->name;
            }
            $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("order_id",$_ro->order_id)->where("status",1)->select("id")->get();
            $temp_amount=0;
            foreach ($before_return_ids as $bri) {
                $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                $temp_amount+= $return_amount[0]->amount;
            }

            $amount=-$temp_amount;
            $_profit_report = array(
                'data_type' => "normal_type",
                'order_id' => $_ro->order_id, //派工单号
                'return_order_id'=>$_ro->id,
                'car_no' => $_ro->car_no, //车牌号
                'name' => $_ro->name, //客户
                'car_model' => $_ro->car_model_name, //车型
                'vin_code' => isset($user_car_info)?$user_car_info->vin_code:'', //车架号
                'order_amount' => -$temp_ro->amount, //维修收入  这个地方是退了多少钱
                'item_amount' => $amount, //配件成本 这个地方是退了的配件成本
                'profit' => -$temp_ro->amount - $amount, //单车毛利
                'profit_rate' => "" , //毛利率
                'phone' => $_ro->phone, //电话
                'user_type' => $user->type, //车辆类型
                'mileage' => isset($user_car_info)?$user_car_info->mileage:'/', //行驶里程
                'engineer_id' => $order_engineer_name, //修理工
                'pay_time' => $_ro->pay_time, //结算日期
                'order_type' => $_ro->order_type, //订单类型
                'insurance' => isset($user_car_info)?$user_car_info->insurance:'', //保险到期日
                "remark"=>"",
                'banjin_supply_amount'=>0,
            );
            array_push($profit_report,$_profit_report);
        }
        //洗车单 退货
        $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
            ->leftJoin('users', 'car_wash_order.user_id', '=', 'users.id')
            ->where('return_carwash_order.status',1)
            ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
            ->where('car_wash_order.shop_id',$shop_id)
            ->select(array('return_carwash_order.id',"car_wash_order.*","users.real_name","users.type"))
            ->get();
        foreach($wash_os_r as $wor){
            if(!is_null($wor->engineer_id)) {
                $order_engineer = MaintenanceEngineers::where('id', $wor->engineer_id)->where('status', 1)->first();
                if($order_engineer) {
                    $en_name = $order_engineer->name;
                }else{
                    $en_name = "";
                }
            }else{
                $en_name = "";
            }
            $_work_hour_report_2 = array(
                'data_type' => 'wash_order_return',
                'order_id' => $wor->order_id, //派工单号
                'return_order_id' => $wor->id, //派工单号
                'car_no' => $wor->car_no, //车牌号
                'name' => $wor->real_name,   //客户名称
                'car_model' => '',   //
                'vin_code' => '',   //
                'order_amount' => -$wor->pay_amount,   //
                'item_amount' => 0, //配件成本
                'profit' => -$wor->pay_amount, //单车毛利
                'profit_rate' => '', //单车毛利率
                'phone' => '', //电话
                'user_type' => $wor->type, //车辆类型
                'mileage' => '',
                'engineer_id' => $en_name, //修理工
                'pay_time' => $wor->pay_time, //结算日期
                'order_type' => 3, //订单类型
                'insurance' => '', //保险到期日
                "remark"=>'',
                'has_banji'=>0,//包含不包含钣金喷漆
                'banjin_supply_amount'=>0,
            );
            array_push($profit_report,$_work_hour_report_2);
        }

        //套餐卡 退货
        $wash_os_r = ReturnServiceCarOrder::leftJoin("service_card_order","service_card_order.order_id","=","return_service_card_order.order_id")
            ->leftJoin('users', 'service_card_order.user_id', '=', 'users.id')
            ->where('return_service_card_order.status',1)
            ->whereBetween('return_service_card_order.implement_time',array($begin_date,$end_date))
            ->where('service_card_order.shop_id',$shop_id)
            ->select(array('return_service_card_order.id',"service_card_order.*",'users.phone','users.real_name','users.type'))
            ->get();
        foreach($wash_os_r as $wor){
            $en_name = "";
            $_work_hour_report_2 = array(
                'data_type' => 'sc_order_return',
                'order_id' => $wor->order_id, //派工单号
                'return_order_id' => $wor->id, //派工单号
                'car_no' => $wor->car_no, //车牌号
                'name' => $wor->real_name,   //客户名称
                'car_model' => '',   //
                'vin_code' => '',   //
                'order_amount' => -$wor->pay_amount,   //
                'item_amount' => 0, //配件成本
                'profit' => -$wor->pay_amount, //单车毛利
                'profit_rate' => '', //单车毛利率
                'phone' => $wor->phone, //电话
                'user_type' => $wor->type, //车辆类型
                'mileage' => '',
                'engineer_id' => $en_name, //修理工
                'pay_time' => $wor->pay_time, //结算日期
                'order_type' => 4, //订单类型
                'insurance' => '', //保险到期日
                "remark"=>'',
                'has_banji'=>0,//包含不包含钣金喷漆
                'banjin_supply_amount'=>0,
            );
            array_push($profit_report,$_work_hour_report_2);
        }
        return $profit_report;
    }

    //盘点查询详情
    public static function get_check_inventory_supply_detail($begin_date,$end_date,$shop_id){
        $check_inventory_supply = array();
        $begin_date=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));
        $inventory_supply = MaintenanceInventorySupply::where('shop_id',$shop_id)
            ->where('status',2)
            ->where('number',"!=",0)
            ->whereBetween('created_at',array($begin_date,$end_date))
            ->get();
        foreach ($inventory_supply as $_inventory_supply){
            $item_detail = Report::get_supply_detail($_inventory_supply->item_id,$_inventory_supply->item_type);
            $part_classification = Report::get_part_classification($item_detail->st_classifi_id);
            $maintenance_inventory_storage = MaintenanceInventoryStorage::where('mis_id',$_inventory_supply->id)
                ->where('status',1)
                ->get();
            foreach ($maintenance_inventory_storage as $_maintenance_inventory_storage){
                $maintenance_storage_item = MaintenanceStorageItem::where('id',$_maintenance_inventory_storage->storage_id)->first();
                $_check_inventory_supply = array(
                    'list_id' => $_inventory_supply->list_id, //单号
                    'created_at' => $_inventory_supply->created_at, //日期
                    'date_id' => $_inventory_supply->date_id, //库房
                    'code' => $item_detail->code, //商品原编码
                    'id' => $item_detail->id, //零件编号
                    'name' => $item_detail->model, //零件名称
                    'specification' => $item_detail->specification, //规格
                    'bz_unit' => $item_detail->bz_unit, //单位
                    'st_classifi_id' => $item_detail->st_classifi_id, //分类号
                    'date_number' => $_inventory_supply->date_number, //帐面数
                    'current_number' => $_inventory_supply->current_number, //盘点数
                    'number' => $_inventory_supply->number, //差异数
                    'storage_id' => $_maintenance_inventory_storage->id,
                    'item_id' =>  $maintenance_storage_item->item_id,
                    'storage_num' => $_maintenance_inventory_storage->number,
                    'tax_purchase_price' => $maintenance_storage_item->tax_purchase_price,
                    'tax_purchase_subtotal' => abs($_maintenance_inventory_storage->number) * $maintenance_storage_item->tax_purchase_price,
                    'level_1_id' => $part_classification['level_1_id'],
                    'level_1_name' => $part_classification['level_1_name'],
                    'level_2_id' => $part_classification['level_2_id'],
                    'level_2_name' => $part_classification['level_2_name'],
                    'level_3_id' => $part_classification['level_3_id'],
                    'level_3_name' => $part_classification['level_3_name'],
                    //进价--存在不同的批次,因此进价不一样.
                    //小计
                    //备注
                    //操作员
                );
                array_push($check_inventory_supply,$_check_inventory_supply);
            }
        }
        return $check_inventory_supply;
    }

    //盘点查询
    public static function get_check_inventory_supply($begin_date,$end_date,$shop_id){
        $check_inventory_supply = array();
        $begin_date=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));
        $inventory_supply = MaintenanceInventorySupply::where('shop_id',$shop_id)
            ->where('status',2)
           // ->where('number',"!=",0)
            ->whereBetween('created_at',array($begin_date,$end_date))
            ->orderBy('implement_time',"asc")
            ->get();
        foreach ($inventory_supply as $_inventory_supply){
            $item_detail = Report::get_supply_detail($_inventory_supply->item_id,$_inventory_supply->item_type);
            $part_classification = Report::get_part_classification($item_detail->supply_classification_id);
            $mdb=MainDateBase::find($_inventory_supply->date_id);
            $amount=DB::select("SELECT sum(mis.number*msi.tax_purchase_price) amount FROM `maintenance_inventory_storage` mis  LEFT JOIN maintenance_storage_item msi on msi.id=mis.storage_id where mis.mis_id=".$_inventory_supply->id);
            $unit = DataSupplyUnit::find($item_detail->unit_id);
            $_check_inventory_supply = array(
                'list_id' => $_inventory_supply->list_id, //单号
                'created_at' => $_inventory_supply->created_at, //日期
                'date_id' => $_inventory_supply->date_id, //库房
                'date_name' => $mdb->name, //库房
                'date_number' => $_inventory_supply->date_number, //帐面数
                'current_number' => $_inventory_supply->current_number, //盘点数
                'number' => $_inventory_supply->number, //差异数
                'code' => $item_detail->code, //商品原编码
                'id' => $item_detail->id, //零件编号
                'name' => $item_detail->name, //零件名称
               // 'specification' => $item_detail->specification, //规格
                'item_id' => $_inventory_supply->item_id, //id
                'item_type' => $_inventory_supply->item_type, //type
                'bz_unit' => $unit ? $unit->name : '', //单位
                'st_classifi_id' => $item_detail->supply_classification_id, //分类号
                "amount"=>$amount[0]->amount,
                'level_1_id' => $part_classification['level_1_id'],
                'level_1_name' => $part_classification['level_1_name'],
                'level_2_id' => $part_classification['level_2_id'],
                'level_2_name' => $part_classification['level_2_name'],
                'level_3_id' => $part_classification['level_3_id'],
                'level_3_name' => $part_classification['level_3_name'],
                //进价--存在不同的批次,因此进价不一样.
                //小计
                //备注
                //操作员
            );
            array_push($check_inventory_supply,$_check_inventory_supply);
        }
        return $check_inventory_supply;
    }

    //维修商品出库明细
    public static function get_export_detail($begin_date,$end_date,$shop_id){
        $export_detail = array();
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            //查找领料 或者退货的  因为 存在结算后退货的情况
            $order_maintenance_item = MaintenanceOutOrder::where('order_id',$_order->order_id)
                ->whereIn('status',array(2,3))
                ->get();
            $order_engineer = MaintenanceEngineers::find($_order->engineer_id);
            $order_car_model = DataCarStandardInfo::find($_order->car_model_id);
            $car_model_series_name = '';
            $car_model_year = '';
            $car_model_series_level = '';
            if($order_car_model || $_order->order_type != 2){
                $car_model_series_name = $order_car_model->series_name;
                $car_model_year = $order_car_model->model_year;
                $car_model_series_level = $order_car_model->series_level;
            }
            foreach ($order_maintenance_item as $_order_maintenance_item){
                //结算后退货的数量
                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_order_maintenance_item->id)->where("status",1)->sum("number");
                if($before_return==0&&$_order_maintenance_item->status==3){//如果是 这个出库申请是结算前 退货完成的  不统计
                    continue;
                }
               // if($_order_maintenance_item->type==0){
               //     $item_info=SupplyItem::getCacheItemInfo($_order_maintenance_item->item_id);
               // }else{
               //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_order_maintenance_item->item_id);
               // }
               // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                $item_info = SupplyItem::find($_order_maintenance_item->item_id);
                $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                $maintenance_out_item = MaintenanceOutItem::where('order_out_id',$_order_maintenance_item->id)
                    ->where('status',1)
                    ->get();//查找出库批次
                switch ($_order_maintenance_item->product_type) {
                    case 'enterprise':
                        $order_product_item = OrderProductEsItem::find($_order_maintenance_item->product_item_id);
                        $opep=OrderProductEsPackage::find($order_product_item->order_product_package_id);
                        $product=ProductPackage::find($opep->package_id);
                        $st=$product->classify;
                        $product_engineer_id=$opep->engineer_id;
                        break;
                    case 'activity':
                        $order_product_item = OrderProductAcItem::find($_order_maintenance_item->product_item_id);
                        $opep=OrderProductAcPackage::find($order_product_item->order_product_package_id);
                        $product=ProductPackage::find($opep->package_id);
                        $st=$product->classify;
                        $product_engineer_id=$opep->engineer_id;
                        break;
                    case 'promotion':
                        $order_product_item = OrderProductItem::find($_order_maintenance_item->product_item_id);
                        $opep=OrderProductPackage::find($order_product_item->order_product_package_id);
                        $product=ProductPackage::find($opep->package_id);
                        $st=$product->classify;
                        $product_engineer_id=$opep->engineer_id;
                        break;
                    case 'base':
                        $order_product_item = OrderProductBaseItem::find($_order_maintenance_item->product_item_id);
                        $op=OrderProduct::find($order_product_item->order_product_package_id);
                        $product=ProductPackage::find($op->product_id);
                        $st=$product->classify;
                        $product_engineer_id=$op->engineer_id;
                        break;
                    case 'non_standard':
                        $order_product_item = OrderProductNonStandard::find($_order_maintenance_item->product_item_id);
                        $op=OrderProduct::find($order_product_item->order_product_id);
                        $s=Service::find($op->product_id);
                        $st=$s->classify;
                        $product_engineer_id=$op->engineer_id;
                        break;
                    case 'service_card':
                        $order_product_item = OrderProductScItem::find($_order_maintenance_item->product_item_id);
                        $op=OrderProduct::find($order_product_item->order_product_id);
                        $product=ProductPackage::find($op->product_id);
                        $st=$product->classify;
                        $product_engineer_id=$op->engineer_id;
                        break;
                }
                if($product_engineer_id==0){
                    $product_engineer=$order_engineer;
                }else{
                    $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                }
                $temp_part_classification = Report::get_service_classification($st);
                //查看 该出库单 结算后退库 多少
                $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_order_maintenance_item->id)->where("status",1)->get();
                $before_return_array=array();//出库批次为键 退了多少为值的 数组
                foreach ($before_return as $_br) {
                    $temp_msi=MaintenanceStorageItem::where("return_order_id",$_br->id)->where("status",1)->get();
                    foreach ($temp_msi as $_temp_msi) {
                        // $before_return_array[$temp_msi->out_id]=$temp_msi->number;
                        if(isset($before_return_array[$_temp_msi->out_id])){
                            $before_return_array[$_temp_msi->out_id]=$before_return_array[$_temp_msi->out_id]+$_temp_msi->number;
                        }else{
                            $before_return_array[$_temp_msi->out_id]=$_temp_msi->number;
                        }
                    }
                }
                // $item_detail = Report::get_supply_detail($_order_maintenance_item->item_id,$_order_maintenance_item->type);
                foreach ($maintenance_out_item as $_maintenance_out_item){
                    $e_number=isset($before_return_array[$_maintenance_out_item->id])?$before_return_array[$_maintenance_out_item->id]:0;//该批次的结算后退货 多少个
                    $number=$_maintenance_out_item->number-$_maintenance_out_item->return_numer+$e_number;
                    $all_amount=($order_product_item->amount/100)*$number;
                    $amount=($order_product_item->counter_amount/100)*$number;
                    $tax_purchase_price=($_maintenance_out_item->tax_purchase_price / 100)*$number;
                    $maintenance_out_list = MaintenanceOutList::where('id',$_maintenance_out_item->list_id)->first();
                    $msll = MaintenanceStorageList::find($_maintenance_out_item->storage_list_id);
                    if($msll){
                        $storage_supplier = MaintenanceSupplier::find($msll->supplier);
                    }
                    $_export_detail = array(
                        'item_type' => $_order_maintenance_item->type, //耗材类型
                        'item_id' => $item_info->id, //耗材id
                        'brand_name' => $item_info->brand_name, //耗材品牌
                        'series_name' => $item_info->series_name, //耗材系列
                        'model' => $item_info->model, //型号
                        'pack' => $item_info->volume_size, //包装数量
                        'supply_name' => $item_info->name, //包装数量
                       // 'unit' => $item_info->unit, //售卖单位
                        'order_type' => $_order->order_type, //
                        'name' => $_order->name, //客户单位
                        'order_id' => $_order->order_id, //派工单号
                        'pay_time' => $_order->pay_time, //结算日期
                        'car_no' => $_order->car_no, //车牌号
                        'car_model' => $_order->car_brand_name, //车型
                        'car_model_name' => $_order->car_model_name, //车型
                        'car_model_id' => $_order->car_model_id, //车型
                        'car_model_series_name' => $car_model_series_name, //车系－名称
                        'car_model_year' => $car_model_year, //车型－年款
                        'car_model_series_level' => $car_model_series_level, //车系－级别
                        'out_date' => $maintenance_out_list->created_at, //出库日期
                        'itme_code' => $_maintenance_out_item->code, //商品编号
                        'item_name' => $_maintenance_out_item->name, //商品名称
                       // 'specification' => $item_info->specification, //耗材规格型号
                       // 'bz_unit' => $item_info->bz_unit, //耗材单位
                        'number' => $number, //数量
                        'amount' => $amount, //金额
                        'all_amount' => $all_amount, //原价金额
                        'tax_purchase_price' => $tax_purchase_price, //成本
                        'profit' => $amount-$tax_purchase_price, //利润
                        'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                        'type' => $maintenance_out_list->type, //出库类型
                        'discount' => $order_product_item->discount, //配件优惠
                        'item_status' => $_maintenance_out_item->status, //领料否
                        'order_status' => $_order->status, //修理状态
                        'storage_list_id' => $_maintenance_out_item->storage_list_id, //入库批次
                        'storage_supplier_name' => isset($storage_supplier) ? $storage_supplier->name : "", //入库供货商
                        'data_id' => $_maintenance_out_item->data_id, //库房
                        'engineer' => isset($product_engineer)?$product_engineer->id:'', //修理工
                        'engineer_name' => isset($product_engineer)?$product_engineer->name:'', //修理工
                        'order_engineer' => $_order->engineer_id, //结车员
                        'order_engineer_name' => isset($order_engineer)?$order_engineer->name:'', //接车元
                        'level_1_id' => $part_classification['level_1_id'],
                        'level_1_name' => $part_classification['level_1_name'],
                        'level_2_id' => $part_classification['level_2_id'],
                        'level_2_name' => $part_classification['level_2_name'],
                        'level_3_id' => $part_classification['level_3_id'],
                        'level_3_name' => $part_classification['level_3_name'],
                        'order_info' =>$temp_part_classification,
                        'return_type' => 0,
                        //接车员
                        //货位
                        //客户编码
                        //是否收费
                        //部门
                        //商品原编码
                    );
                    array_push($export_detail,$_export_detail);
                }
            }
        }
        //计算这个时间段内 退货的 记录

        $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
            ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id',"orders.*"))->get();
        foreach ($ro as $_ro){
            $order_maintenance_item = MaintenanceOutOrder::where('order_id',$_ro->order_id)
                ->whereIn('status',array(2,3))
                ->first();
            if(is_null($order_maintenance_item)){
                continue;
            }
            $order_engineer = MaintenanceEngineers::find($_ro->engineer_id);
            $order_car_model_1 = DataCarStandardInfo::find($_ro->car_model_id);
            $car_model_series_name = '';
            $car_model_year = '';
            $car_model_series_level = '';
            if($order_car_model_1 || $_ro->order_type != 2){
                $car_model_series_name = $order_car_model_1->series_name;
                $car_model_year = $order_car_model_1->model_year;
                $car_model_series_level = $order_car_model_1->series_level;
            }
            $roi=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->where("type",3)->first();//说明是 均摊的退货
            if($roi){
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",3)->where("type",3)->get();
            }else{
                $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->whereIn("type",array(0,1))->get();//
            }
            foreach ($ori as $_roi) {
                if ($_roi->type != 4) {
                    $temp_item = new Stdclass;
                    switch ($_roi->product_type) {
                        case 'enterprise':
                            $order_product_item = OrderProductEsItem::find($_roi->order_product_item_id);
                            if ($order_product_item->type == 2) {
                                continue 2;
                            }
                            $temp_item->type = $order_product_item->item_type;
                            $temp_item->item_id = $order_product_item->item_id;
                            $opep = OrderProductEsPackage::find($order_product_item->order_product_package_id);
                            $product = ProductPackage::find($opep->package_id);
                            $st = $product->classify;
                            $product_engineer_id = $opep->engineer_id;
                            break;
                        case 'activity':
                            $order_product_item = OrderProductAcItem::find($_roi->order_product_item_id);
                            if ($order_product_item->type == 2) {
                                continue 2;
                            }
                            $temp_item->type = $order_product_item->item_type;
                            $temp_item->item_id = $order_product_item->item_id;
                            $opep = OrderProductAcPackage::find($order_product_item->order_product_package_id);
                            $product = ProductPackage::find($opep->package_id);
                            $st = $product->classify;
                            $product_engineer_id = $opep->engineer_id;
                            break;
                        case 'promotion':
                            // $order_product_item = OrderProductItem::find($_roi->order_product_item_id);
                            // $opep=OrderProductPackage::find($order_product_item->order_product_package_id);
                            // $product=ProductPackage::find($opep->package_id);
                            // $st=$product->classify;
                            // $product_engineer_id=$opep->engineer_id;
                            break;
                        case 'base':
                            $order_product_item = OrderProductBaseItem::find($_roi->order_product_item_id);
                            if ($order_product_item->type == 2) {
                                continue 2;
                            }
                            $temp_item->type = $order_product_item->item_type;
                            $temp_item->item_id = $order_product_item->item_id;
                            $op = OrderProduct::find($order_product_item->order_product_package_id);
                            $product = ProductPackage::find($op->product_id);
                            $st = $product->classify;
                            $product_engineer_id = $op->engineer_id;
                            break;
                        case 'non_standard':
                            $order_product_item = OrderProductNonStandard::find($_roi->order_product_item_id);
                            if ($order_product_item->service_type == 2) {
                                continue 2;
                            }
                            $temp_item->type = $order_product_item->type;
                            $temp_item->item_id = $order_product_item->item_id;
                            $op = OrderProduct::find($order_product_item->order_product_id);
                            $s = Service::find($op->product_id);
                            $st = $s->classify;
                            $product_engineer_id = $op->engineer_id;
                            break;
                        case 'service_card':
                            $order_product_item = OrderProductScItem::find($_roi->order_product_item_id);
                            if ($order_product_item->type == 2) {
                                continue 2;
                            }
                            $temp_item->type = $order_product_item->item_type;
                            $temp_item->item_id = $order_product_item->item_id;
                            $op = OrderProduct::find($order_product_item->order_product_id);
                            $product = ProductPackage::find($op->product_id);
                            $st = $product->classify;
                            $product_engineer_id = $op->engineer_id;
                            break;
                    }
                    // if($_order_maintenance_item->type==0){
                    //     $item_info=SupplyItem::getCacheItemInfo($_order_maintenance_item->item_id);
                    // }else{
                    //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_order_maintenance_item->item_id);
                    // }
                    // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                        $item_info = SupplyItem::find($temp_item->item_id);
                        $part_classification = Report::get_part_classification($item_info->supply_classification_id);

                    if ($product_engineer_id == 0) {
                        $product_engineer = $order_engineer;
                    } else {
                        $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                    }
                    $temp_part_classification = Report::get_service_classification($st);
                    $b_number = 0;
                    $b_tax_purchase_price = 0;
                    if ($_roi->type == 0) {//如果是 退货
                        $b_number = -$_roi->number;
                        $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
                        foreach ($before_return as $_br) {
                            $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                            foreach ($temp_msi as $_temp_msi) {
                                $temp_moi = MaintenanceOutItem::find($_temp_msi->out_id);
                                $b_tax_purchase_price = $b_tax_purchase_price + ($_temp_msi->number * $temp_moi->tax_purchase_price);
                            }
                        }
                    }
                    //查找入库信息
                    $sli = array();
                    $sli_s = "";
                    $ss = "";
                    $moo1 = MaintenanceOutOrder::where('order_id', $_ro->order_id)->where('product_type', $_roi->product_type)->where('product_item_id', $_roi->order_product_item_id)->select('id')->get();
                    foreach ($moo1 as $mooo2) {
                        $moi1 = MaintenanceOutItem::where('order_out_id', $mooo2->id)->where('item_type', $temp_item->type)->where('item_id', $temp_item->item_id)->select('storage_list_id')->get();
                        foreach ($moi1 as $moi2) {
                            $sli[] = $moi2->storage_list_id;
                        }
                    }
                    $sli = array_flip(array_flip($sli));
                    foreach ($sli as $sli1) {
                        $msl2 = MaintenanceStorageList::find($sli1);
                        $sli_s .= $sli1 . "/";
                        if ($msl2) {
                            $storage_supplier = MaintenanceSupplier::find($msl2->supplier);
                            if ($storage_supplier) {
                                $ss .= $storage_supplier->name . "/";
                            } else {
                                $ss .= "/";
                            }
                        } else {
                            $ss .= "/";
                        }
                    }
                    $sli_s = substr($sli_s, 0, strlen($sli_s) - 1);
                    $ss = substr($ss, 0, strlen($ss) - 1);
                    $_export_detail = array(
                        'item_type' => $temp_item->type, //耗材类型
                        'item_id' => $item_info->id, //耗材id
                        'brand_name' => $item_info->brand_name, //耗材品牌
                        'series_name' => $item_info->series_name, //耗材系列
                        'model' => $item_info->model, //型号
                        'pack' => $item_info->volume_size, //包装数量
                        'supply_name' => $item_info->name,
                       // 'unit' => $item_info->unit, //售卖单位
                        'order_type' => $_ro->order_type, //客户单位
                        'name' => $_ro->name, //客户单位
                        'order_id' => $_ro->order_id, //派工单号
                        'pay_time' => $_ro->pay_time, //结算日期
                        'car_no' => $_ro->car_no, //车牌号
                        'car_model' => $_ro->car_brand_name, //车型
                        'car_model_name' => $_ro->car_model_name, //车型
                        'car_model_id' => $_ro->car_model_id, //车型
                        'car_model_series_name' => $car_model_series_name, //车系－名称
                        'car_model_year' => $car_model_year, //车型－年款
                        'car_model_series_level' => $car_model_series_level, //车系－级别
                        'out_date' => '', //出库日期
                        'itme_code' => $item_info->code, //商品编号
                        'item_name' => $order_product_item->name, //商品名称
                       // 'specification' => $item_info->specification, //耗材规格型号
                       // 'bz_unit' => $item_info->bz_unit, //耗材单位
                        'number' => $b_number, //数量
                        'amount' => -$_roi->return_amount / 100, //金额
                        'all_amount' => 0, //原价金额
                        'tax_purchase_price' => -$b_tax_purchase_price / 100, //成本
                        'profit' => (-$_roi->return_amount + $b_tax_purchase_price) / 100, //利润
                        'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                        'type' => -1, //出库类型
                        'discount' => 0, //配件优惠
                        'item_status' => -1, //领料否
                        'order_status' => $_ro->status, //修理状态
                        'storage_list_id' => $sli_s, //入库批次
                        'storage_supplier_name' => $ss, //入库供货商
                        'data_id' => '', //库房
                        'engineer' => isset($product_engineer) ? $product_engineer->id : '', //修理工
                        'engineer_name' => isset($product_engineer) ? $product_engineer->name : '', //修理工
                        'order_engineer' => $_ro->engineer_id, //结车员
                        'order_engineer_name' => isset($order_engineer) ? $order_engineer->name : '', //接车元
                        'level_1_id' => $part_classification['level_1_id'],
                        'level_1_name' => $part_classification['level_1_name'],
                        'level_2_id' => $part_classification['level_2_id'],
                        'level_2_name' => $part_classification['level_2_name'],
                        'level_3_id' => $part_classification['level_3_id'],
                        'level_3_name' => $part_classification['level_3_name'],
                        'order_info' => $temp_part_classification,
                        'return_type' => 1,
                    );
                    array_push($export_detail, $_export_detail);
                }else{
                    $ops = OrderProduct::find($_roi->order_product_id);
                    if ($ops->product_type == 'enterprise') {//说明是 企业报
                        $opep = OrderProductEsPackage::where("order_product_id", $_roi->order_product_id)->get();
                        foreach ($opep as $_opep) {
                            $product = ProductPackage::find($_opep->package_id);
                            $st = $product->classify;
                            $product_engineer_id = $_opep->engineer_id;
                            $item = OrderProductEsItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
                            foreach($item as $_it){
                                if ($_it->type == 2) {
                                    continue 2;
                                }
                                $temp_item_type = $_it->item_type;
                                $temp_item_item_id = $_it->item_id;
                               // if ($temp_item_type == 0) {
                               //     $item_info = SupplyItem::getCacheItemInfo($temp_item_item_id);
                               // } else {
                               //     $item_info = SupplyItemNonStandard::getCacheItemInfo($temp_item_item_id);
                               // }
                               // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                                $item_info = SupplyItem::find($temp_item_item_id);
                                $part_classification = Report::get_part_classification($item_info->supply_classification_id);

                                if ($product_engineer_id == 0) {
                                    $product_engineer = $order_engineer;
                                } else {
                                    $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                                }
                                $temp_part_classification = Report::get_service_classification($st);
                                $b_number = 0;
                                $b_tax_purchase_price = 0;
                                if ($_roi->type == 0) {//如果是 退货
                                    $b_number = -$_roi->number;
                                    $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
                                    foreach ($before_return as $_br) {
                                        $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                        foreach ($temp_msi as $_temp_msi) {
                                            $temp_moi = MaintenanceOutItem::find($_temp_msi->out_id);
                                            $b_tax_purchase_price = $b_tax_purchase_price + ($_temp_msi->number * $temp_moi->tax_purchase_price);
                                        }
                                    }
                                }
                                //查找入库信息
                                $sli = array();
                                $sli_s = "";
                                $ss = "";
                                $moo1 = MaintenanceOutOrder::where('order_id', $_ro->order_id)->where('product_type', $_roi->product_type)->where('product_item_id', $_roi->order_product_item_id)->select('id')->get();
                                foreach ($moo1 as $mooo2) {
                                    $moi1 = MaintenanceOutItem::where('order_out_id', $mooo2->id)->where('item_type', $temp_item_type)->where('item_id', $temp_item_item_id)->select('storage_list_id')->get();
                                    foreach ($moi1 as $moi2) {
                                        $sli[] = $moi2->storage_list_id;
                                    }
                                }
                                $sli = array_flip(array_flip($sli));
                                foreach ($sli as $sli1) {
                                    $msl2 = MaintenanceStorageList::find($sli1);
                                    $sli_s .= $sli1 . "/";
                                    if ($msl2) {
                                        $storage_supplier = MaintenanceSupplier::find($msl2->supplier);
                                        if ($storage_supplier) {
                                            $ss .= $storage_supplier->name . "/";
                                        } else {
                                            $ss .= "/";
                                        }
                                    } else {
                                        $ss .= "/";
                                    }
                                }
                                $sli_s = substr($sli_s, 0, strlen($sli_s) - 1);
                                $ss = substr($ss, 0, strlen($ss) - 1);
                                $_export_detail = array(
                                    'item_type' => $temp_item_type, //耗材类型
                                    'item_id' => $item_info->id, //耗材id
                                    'brand_name' => $item_info->brand_name, //耗材品牌
                                    'series_name' => $item_info->series_name, //耗材系列
                                    'model' => $item_info->model, //型号
                                    'pack' => $item_info->volume_size, //包装数量
                                    'supply_name' => $item_info->name,
                                   // 'unit' => $item_info->unit, //售卖单位
                                    'order_type' => $_ro->order_type, //客户单位
                                    'name' => $_ro->name, //客户单位
                                    'order_id' => $_ro->order_id, //派工单号
                                    'pay_time' => $_ro->pay_time, //结算日期
                                    'car_no' => $_ro->car_no, //车牌号
                                    'car_model' => $_ro->car_brand_name, //车型
                                    'car_model_name' => $_ro->car_model_name, //车型
                                    'car_model_id' => $_ro->car_model_id, //车型
                                    'car_model_series_name' => $car_model_series_name, //车系－名称
                                    'car_model_year' => $car_model_year, //车型－年款
                                    'car_model_series_level' => $car_model_series_level, //车系－级别
                                    'out_date' => '', //出库日期
                                    'itme_code' => $item_info->code, //商品编号
                                    'item_name' => $_it->item_name, //商品名称
                                   // 'specification' => $item_info->specification, //耗材规格型号
                                   // 'bz_unit' => $item_info->bz_unit, //耗材单位
                                    'number' => $b_number, //数量
                                    'amount' => -$_roi->return_amount / 100, //金额
                                    'all_amount' => 0, //原价金额
                                    'tax_purchase_price' => -$b_tax_purchase_price / 100, //成本
                                    'profit' => (-$_roi->return_amount + $b_tax_purchase_price) / 100, //利润
                                    'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                                    'type' => -1, //出库类型
                                    'discount' => 0, //配件优惠
                                    'item_status' => -1, //领料否
                                    'order_status' => $_ro->status, //修理状态
                                    'storage_list_id' => $sli_s, //入库批次
                                    'storage_supplier_name' => $ss, //入库供货商
                                    'data_id' => '', //库房
                                    'engineer' => isset($product_engineer) ? $product_engineer->id : '', //修理工
                                    'engineer_name' => isset($product_engineer) ? $product_engineer->name : '', //修理工
                                    'order_engineer' => $_ro->engineer_id, //结车员
                                    'order_engineer_name' => isset($order_engineer) ? $order_engineer->name : '', //接车元
                                    'level_1_id' => $part_classification['level_1_id'],
                                    'level_1_name' => $part_classification['level_1_name'],
                                    'level_2_id' => $part_classification['level_2_id'],
                                    'level_2_name' => $part_classification['level_2_name'],
                                    'level_3_id' => $part_classification['level_3_id'],
                                    'level_3_name' => $part_classification['level_3_name'],
                                    'order_info' => $temp_part_classification,
                                    'return_type' => 1,
                                );
                                array_push($export_detail, $_export_detail);
                            }
                        }
                    } elseif ($ops->product_type == 'activity') {//说明是 企业报
                        $opep = OrderProductAcPackage::where("order_product_id", $_roi->order_product_id)->get();
                        foreach ($opep as $_opep) {
                            $product = ProductPackage::find($_opep->package_id);
                            $st = $product->classify;
                            $product_engineer_id = $_opep->engineer_id;
                            $item = OrderProductEsItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
                            foreach($item as $_it){
                                if ($_it->type == 2) {
                                    continue 2;
                                }
                                $temp_item_type = $_it->item_type;
                                $temp_item_item_id = $_it->item_id;
                               // if ($temp_item_type == 0) {
                               //     $item_info = SupplyItem::getCacheItemInfo($temp_item_item_id);
                               // } else {
                               //     $item_info = SupplyItemNonStandard::getCacheItemInfo($temp_item_item_id);
                               // }
                               // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                                $item_info = SupplyItem::find($temp_item_item_id);
                                $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                                if ($product_engineer_id == 0) {
                                    $product_engineer = $order_engineer;
                                } else {
                                    $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                                }
                                $temp_part_classification = Report::get_service_classification($st);
                                $b_number = 0;
                                $b_tax_purchase_price = 0;
                                if ($_roi->type == 0) {//如果是 退货
                                    $b_number = -$_roi->number;
                                    $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
                                    foreach ($before_return as $_br) {
                                        $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                        foreach ($temp_msi as $_temp_msi) {
                                            $temp_moi = MaintenanceOutItem::find($_temp_msi->out_id);
                                            $b_tax_purchase_price = $b_tax_purchase_price + ($_temp_msi->number * $temp_moi->tax_purchase_price);
                                        }
                                    }
                                }
                                //查找入库信息
                                $sli = array();
                                $sli_s = "";
                                $ss = "";
                                $moo1 = MaintenanceOutOrder::where('order_id', $_ro->order_id)->where('product_type', $_roi->product_type)->where('product_item_id', $_roi->order_product_item_id)->select('id')->get();
                                foreach ($moo1 as $mooo2) {
                                    $moi1 = MaintenanceOutItem::where('order_out_id', $mooo2->id)->where('item_type', $temp_item_type)->where('item_id', $temp_item_item_id)->select('storage_list_id')->get();
                                    foreach ($moi1 as $moi2) {
                                        $sli[] = $moi2->storage_list_id;
                                    }
                                }
                                $sli = array_flip(array_flip($sli));
                                foreach ($sli as $sli1) {
                                    $msl2 = MaintenanceStorageList::find($sli1);
                                    $sli_s .= $sli1 . "/";
                                    if ($msl2) {
                                        $storage_supplier = MaintenanceSupplier::find($msl2->supplier);
                                        if ($storage_supplier) {
                                            $ss .= $storage_supplier->name . "/";
                                        } else {
                                            $ss .= "/";
                                        }
                                    } else {
                                        $ss .= "/";
                                    }
                                }
                                $sli_s = substr($sli_s, 0, strlen($sli_s) - 1);
                                $ss = substr($ss, 0, strlen($ss) - 1);
                                $_export_detail = array(
                                    'item_type' => $temp_item_type, //耗材类型
                                    'item_id' => $item_info->id, //耗材id
                                    'brand_name' => $item_info->brand_name, //耗材品牌
                                    'series_name' => $item_info->series_name, //耗材系列
                                    'model' => $item_info->model, //型号
                                    'pack' => $item_info->volume_size, //包装数量
                                    'supply_name' => $item_info->name,
                                   // 'unit' => $item_info->unit, //售卖单位
                                    'order_type' => $_ro->order_type, //客户单位
                                    'name' => $_ro->name, //客户单位
                                    'order_id' => $_ro->order_id, //派工单号
                                    'pay_time' => $_ro->pay_time, //结算日期
                                    'car_no' => $_ro->car_no, //车牌号
                                    'car_model' => $_ro->car_brand_name, //车型
                                    'car_model_name' => $_ro->car_model_name, //车型
                                    'car_model_id' => $_ro->car_model_id, //车型
                                    'car_model_series_name' => $car_model_series_name, //车系－名称
                                    'car_model_year' => $car_model_year, //车型－年款
                                    'car_model_series_level' => $car_model_series_level, //车系－级别
                                    'out_date' => '', //出库日期
                                    'itme_code' => $item_info->code, //商品编号
                                    'item_name' => $_it->item_name, //商品名称
                                   // 'specification' => $item_info->specification, //耗材规格型号
                                   // 'bz_unit' => $item_info->bz_unit, //耗材单位
                                    'number' => $b_number, //数量
                                    'amount' => -$_roi->return_amount / 100, //金额
                                    'all_amount' => 0, //原价金额
                                    'tax_purchase_price' => -$b_tax_purchase_price / 100, //成本
                                    'profit' => (-$_roi->return_amount + $b_tax_purchase_price) / 100, //利润
                                    'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                                    'type' => -1, //出库类型
                                    'discount' => 0, //配件优惠
                                    'item_status' => -1, //领料否
                                    'order_status' => $_ro->status, //修理状态
                                    'storage_list_id' => $sli_s, //入库批次
                                    'storage_supplier_name' => $ss, //入库供货商
                                    'data_id' => '', //库房
                                    'engineer' => isset($product_engineer) ? $product_engineer->id : '', //修理工
                                    'engineer_name' => isset($product_engineer) ? $product_engineer->name : '', //修理工
                                    'order_engineer' => $_ro->engineer_id, //结车员
                                    'order_engineer_name' => isset($order_engineer) ? $order_engineer->name : '', //接车元
                                    'level_1_id' => $part_classification['level_1_id'],
                                    'level_1_name' => $part_classification['level_1_name'],
                                    'level_2_id' => $part_classification['level_2_id'],
                                    'level_2_name' => $part_classification['level_2_name'],
                                    'level_3_id' => $part_classification['level_3_id'],
                                    'level_3_name' => $part_classification['level_3_name'],
                                    'order_info' => $temp_part_classification,
                                    'return_type' => 1,
                                );
                                array_push($export_detail, $_export_detail);
                            }
                        }
                    } elseif ($ops->product_type == 'base') {//说明是 基础
                        $item = OrderProductBaseItem::where("order_product_package_id", $ops->id)->get();
                        $product = ProductPackage::find($ops->product_id);
                        $st = $product->classify;
                        $product_engineer_id = $ops->engineer_id;
                        foreach($item as $_it){
                            if ($_it->type == 2) {
                                continue 2;
                            }
                            $temp_item_type = $_it->item_type;
                            $temp_item_item_id = $_it->item_id;
                           // if ($temp_item_type == 0) {
                           //     $item_info = SupplyItem::getCacheItemInfo($temp_item_item_id);
                           // } else {
                           //     $item_info = SupplyItemNonStandard::getCacheItemInfo($temp_item_item_id);
                           // }
                           // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                            $item_info = SupplyItem::find($temp_item_item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            if ($product_engineer_id == 0) {
                                $product_engineer = $order_engineer;
                            } else {
                                $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                            }
                            $temp_part_classification = Report::get_service_classification($st);
                            $b_number = 0;
                            $b_tax_purchase_price = 0;
                            if ($_roi->type == 0) {//如果是 退货
                                $b_number = -$_roi->number;
                                $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
                                foreach ($before_return as $_br) {
                                    $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                    foreach ($temp_msi as $_temp_msi) {
                                        $temp_moi = MaintenanceOutItem::find($_temp_msi->out_id);
                                        $b_tax_purchase_price = $b_tax_purchase_price + ($_temp_msi->number * $temp_moi->tax_purchase_price);
                                    }
                                }
                            }
                            //查找入库信息
                            $sli = array();
                            $sli_s = "";
                            $ss = "";
                            $moo1 = MaintenanceOutOrder::where('order_id', $_ro->order_id)->where('product_type', $_roi->product_type)->where('product_item_id', $_roi->order_product_item_id)->select('id')->get();
                            foreach ($moo1 as $mooo2) {
                                $moi1 = MaintenanceOutItem::where('order_out_id', $mooo2->id)->where('item_type', $temp_item_type)->where('item_id', $temp_item_item_id)->select('storage_list_id')->get();
                                foreach ($moi1 as $moi2) {
                                    $sli[] = $moi2->storage_list_id;
                                }
                            }
                            $sli = array_flip(array_flip($sli));
                            foreach ($sli as $sli1) {
                                $msl2 = MaintenanceStorageList::find($sli1);
                                $sli_s .= $sli1 . "/";
                                if ($msl2) {
                                    $storage_supplier = MaintenanceSupplier::find($msl2->supplier);
                                    if ($storage_supplier) {
                                        $ss .= $storage_supplier->name . "/";
                                    } else {
                                        $ss .= "/";
                                    }
                                } else {
                                    $ss .= "/";
                                }
                            }
                            $sli_s = substr($sli_s, 0, strlen($sli_s) - 1);
                            $ss = substr($ss, 0, strlen($ss) - 1);
                            $_export_detail = array(
                                'item_type' => $temp_item_type, //耗材类型
                                'item_id' => $item_info->id, //耗材id
                                'brand_name' => $item_info->brand_name, //耗材品牌
                                'series_name' => $item_info->series_name, //耗材系列
                                'model' => $item_info->model, //型号
                                'pack' => $item_info->volume_size, //包装数量
                                'supply_name' => $item_info->name,
                               // 'unit' => $item_info->unit, //售卖单位
                                'order_type' => $_ro->order_type, //客户单位
                                'name' => $_ro->name, //客户单位
                                'order_id' => $_ro->order_id, //派工单号
                                'pay_time' => $_ro->pay_time, //结算日期
                                'car_no' => $_ro->car_no, //车牌号
                                'car_model' => $_ro->car_brand_name, //车型
                                'car_model_name' => $_ro->car_model_name, //车型
                                'car_model_id' => $_ro->car_model_id, //车型
                                'car_model_series_name' => $car_model_series_name, //车系－名称
                                'car_model_year' => $car_model_year, //车型－年款
                                'car_model_series_level' => $car_model_series_level, //车系－级别
                                'out_date' => '', //出库日期
                                'itme_code' => $item_info->code, //商品编号
                                'item_name' => $_it->item_name, //商品名称
                               // 'specification' => $item_info->specification, //耗材规格型号
                               // 'bz_unit' => $item_info->bz_unit, //耗材单位
                                'number' => $b_number, //数量
                                'amount' => -$_roi->return_amount / 100, //金额
                                'all_amount' => 0, //原价金额
                                'tax_purchase_price' => -$b_tax_purchase_price / 100, //成本
                                'profit' => (-$_roi->return_amount + $b_tax_purchase_price) / 100, //利润
                                'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                                'type' => -1, //出库类型
                                'discount' => 0, //配件优惠
                                'item_status' => -1, //领料否
                                'order_status' => $_ro->status, //修理状态
                                'storage_list_id' => $sli_s, //入库批次
                                'storage_supplier_name' => $ss, //入库供货商
                                'data_id' => '', //库房
                                'engineer' => isset($product_engineer) ? $product_engineer->id : '', //修理工
                                'engineer_name' => isset($product_engineer) ? $product_engineer->name : '', //修理工
                                'order_engineer' => $_ro->engineer_id, //结车员
                                'order_engineer_name' => isset($order_engineer) ? $order_engineer->name : '', //接车元
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'order_info' => $temp_part_classification,
                                'return_type' => 1,
                            );
                            array_push($export_detail, $_export_detail);
                        }
                    } elseif ($ops->product_type == 'non_standard') {//说明是 非标
                        $item = OrderProductNonStandard::where("order_product_id", $ops->id)->get();
                        $product = ProductPackage::find($ops->product_id);
                        $st = $product->classify;
                        $product_engineer_id = $ops->engineer_id;
                        foreach($item as $_it){
                            if ($_it->service_type == 2) {
                                continue 2;
                            }
                            // $temp_item_type = $_it->item_type;
                            $temp_item_item_id = $_it->item_id;
                           // if ($temp_item_type == 0) {
                           //     $item_info = SupplyItem::getCacheItemInfo($temp_item_item_id);
                           // } else {
                           //     $item_info = SupplyItemNonStandard::getCacheItemInfo($temp_item_item_id);
                           // }
                           // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                            $item_info = SupplyItem::find($temp_item_item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            if ($product_engineer_id == 0) {
                                $product_engineer = $order_engineer;
                            } else {
                                $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                            }
                            $temp_part_classification = Report::get_service_classification($st);
                            $b_number = 0;
                            $b_tax_purchase_price = 0;
                            if ($_roi->type == 0) {//如果是 退货
                                $b_number = -$_roi->number;
                                $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
                                foreach ($before_return as $_br) {
                                    $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                    foreach ($temp_msi as $_temp_msi) {
                                        $temp_moi = MaintenanceOutItem::find($_temp_msi->out_id);
                                        $b_tax_purchase_price = $b_tax_purchase_price + ($_temp_msi->number * $temp_moi->tax_purchase_price);
                                    }
                                }
                            }
                            //查找入库信息
                            $sli = array();
                            $sli_s = "";
                            $ss = "";
                            $moo1 = MaintenanceOutOrder::where('order_id', $_ro->order_id)->where('product_type', $_roi->product_type)->where('product_item_id', $_roi->order_product_item_id)->select('id')->get();
                            foreach ($moo1 as $mooo2) {
                                $moi1 = MaintenanceOutItem::where('order_out_id', $mooo2->id)->where('item_type', $temp_item_type)->where('item_id', $temp_item_item_id)->select('storage_list_id')->get();
                                foreach ($moi1 as $moi2) {
                                    $sli[] = $moi2->storage_list_id;
                                }
                            }
                            $sli = array_flip(array_flip($sli));
                            foreach ($sli as $sli1) {
                                $msl2 = MaintenanceStorageList::find($sli1);
                                $sli_s .= $sli1 . "/";
                                if ($msl2) {
                                    $storage_supplier = MaintenanceSupplier::find($msl2->supplier);
                                    if ($storage_supplier) {
                                        $ss .= $storage_supplier->name . "/";
                                    } else {
                                        $ss .= "/";
                                    }
                                } else {
                                    $ss .= "/";
                                }
                            }
                            $sli_s = substr($sli_s, 0, strlen($sli_s) - 1);
                            $ss = substr($ss, 0, strlen($ss) - 1);
                            $_export_detail = array(
                                'item_type' => $temp_item_type, //耗材类型
                                'item_id' => $item_info->id, //耗材id
                                'brand_name' => $item_info->brand_name, //耗材品牌
                                'series_name' => $item_info->series_name, //耗材系列
                                'model' => $item_info->model, //型号
                                'pack' => $item_info->volume_size, //包装数量
                                'supply_name' => $item_info->name,
                               // 'unit' => $item_info->unit, //售卖单位
                                'order_type' => $_ro->order_type, //客户单位
                                'name' => $_ro->name, //客户单位
                                'order_id' => $_ro->order_id, //派工单号
                                'pay_time' => $_ro->pay_time, //结算日期
                                'car_no' => $_ro->car_no, //车牌号
                                'car_model' => $_ro->car_brand_name, //车型
                                'car_model_name' => $_ro->car_model_name, //车型
                                'car_model_id' => $_ro->car_model_id, //车型
                                'car_model_series_name' => $car_model_series_name, //车系－名称
                                'car_model_year' => $car_model_year, //车型－年款
                                'car_model_series_level' => $car_model_series_level, //车系－级别
                                'out_date' => '', //出库日期
                                'itme_code' => $item_info->code, //商品编号
                                'item_name' => $_it->name, //商品名称
                               // 'specification' => $item_info->specification, //耗材规格型号
                               // 'bz_unit' => $item_info->bz_unit, //耗材单位
                                'number' => $b_number, //数量
                                'amount' => -$_roi->return_amount / 100, //金额
                                'all_amount' => 0, //原价金额
                                'tax_purchase_price' => -$b_tax_purchase_price / 100, //成本
                                'profit' => (-$_roi->return_amount + $b_tax_purchase_price) / 100, //利润
                                'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                                'type' => -1, //出库类型
                                'discount' => 0, //配件优惠
                                'item_status' => -1, //领料否
                                'order_status' => $_ro->status, //修理状态
                                'storage_list_id' => $sli_s, //入库批次
                                'storage_supplier_name' => $ss, //入库供货商
                                'data_id' => '', //库房
                                'engineer' => isset($product_engineer) ? $product_engineer->id : '', //修理工
                                'engineer_name' => isset($product_engineer) ? $product_engineer->name : '', //修理工
                                'order_engineer' => $_ro->engineer_id, //结车员
                                'order_engineer_name' => isset($order_engineer) ? $order_engineer->name : '', //接车元
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'order_info' => $temp_part_classification,
                                'return_type' => 1,
                            );
                            array_push($export_detail, $_export_detail);
                        }
                    } elseif ($ops->product_type == 'service_card') {//说明是 套餐卡
                        $item = OrderProductScItem::where("order_product_id", $ops->id)->get();
                        $product = ProductPackage::find($ops->product_id);
                        $st = $product->classify;
                        $product_engineer_id = $ops->engineer_id;
                        foreach($item as $_it){
                            if ($_it->type == 2) {
                                continue 2;
                            }
                            $temp_item_type = $_it->item_type;
                            $temp_item_item_id = $_it->item_id;
                           // if ($temp_item_type == 0) {
                           //     $item_info = SupplyItem::getCacheItemInfo($temp_item_item_id);
                           // } else {
                           //     $item_info = SupplyItemNonStandard::getCacheItemInfo($temp_item_item_id);
                           // }
                           // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                            $item_info = SupplyItem::find($temp_item_item_id);
                            $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                            if ($product_engineer_id == 0) {
                                $product_engineer = $order_engineer;
                            } else {
                                $product_engineer = MaintenanceEngineers::find($product_engineer_id);
                            }
                            $temp_part_classification = Report::get_service_classification($st);
                            $b_number = 0;
                            $b_tax_purchase_price = 0;
                            if ($_roi->type == 0) {//如果是 退货
                                $b_number = -$_roi->number;
                                $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
                                foreach ($before_return as $_br) {
                                    $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
                                    foreach ($temp_msi as $_temp_msi) {
                                        $temp_moi = MaintenanceOutItem::find($_temp_msi->out_id);
                                        $b_tax_purchase_price = $b_tax_purchase_price + ($_temp_msi->number * $temp_moi->tax_purchase_price);
                                    }
                                }
                            }
                            //查找入库信息
                            $sli = array();
                            $sli_s = "";
                            $ss = "";
                            $moo1 = MaintenanceOutOrder::where('order_id', $_ro->order_id)->where('product_type', $_roi->product_type)->where('product_item_id', $_roi->order_product_item_id)->select('id')->get();
                            foreach ($moo1 as $mooo2) {
                                $moi1 = MaintenanceOutItem::where('order_out_id', $mooo2->id)->where('item_type', $temp_item_type)->where('item_id', $temp_item_item_id)->select('storage_list_id')->get();
                                foreach ($moi1 as $moi2) {
                                    $sli[] = $moi2->storage_list_id;
                                }
                            }
                            $sli = array_flip(array_flip($sli));
                            foreach ($sli as $sli1) {
                                $msl2 = MaintenanceStorageList::find($sli1);
                                $sli_s .= $sli1 . "/";
                                if ($msl2) {
                                    $storage_supplier = MaintenanceSupplier::find($msl2->supplier);
                                    if ($storage_supplier) {
                                        $ss .= $storage_supplier->name . "/";
                                    } else {
                                        $ss .= "/";
                                    }
                                } else {
                                    $ss .= "/";
                                }
                            }
                            $sli_s = substr($sli_s, 0, strlen($sli_s) - 1);
                            $ss = substr($ss, 0, strlen($ss) - 1);
                            $_export_detail = array(
                                'item_type' => $temp_item_type, //耗材类型
                                'item_id' => $item_info->id, //耗材id
                                'brand_name' => $item_info->brand_name, //耗材品牌
                                'series_name' => $item_info->series_name, //耗材系列
                                'model' => $item_info->model, //型号
                                'pack' => $item_info->volume_size, //包装数量
                                'supply_name' => $item_info->name,
                               // 'unit' => $item_info->unit, //售卖单位
                                'order_type' => $_ro->order_type, //客户单位
                                'name' => $_ro->name, //客户单位
                                'order_id' => $_ro->order_id, //派工单号
                                'pay_time' => $_ro->pay_time, //结算日期
                                'car_no' => $_ro->car_no, //车牌号
                                'car_model' => $_ro->car_brand_name, //车型
                                'car_model_name' => $_ro->car_model_name, //车型
                                'car_model_id' => $_ro->car_model_id, //车型
                                'car_model_series_name' => $car_model_series_name, //车系－名称
                                'car_model_year' => $car_model_year, //车型－年款
                                'car_model_series_level' => $car_model_series_level, //车系－级别
                                'out_date' => '', //出库日期
                                'itme_code' => $item_info->code, //商品编号
                                'item_name' => $_it->item_name, //商品名称
                               // 'specification' => $item_info->specification, //耗材规格型号
                               // 'bz_unit' => $item_info->bz_unit, //耗材单位
                                'number' => $b_number, //数量
                                'amount' => -$_roi->return_amount / 100, //金额
                                'all_amount' => 0, //原价金额
                                'tax_purchase_price' => -$b_tax_purchase_price / 100, //成本
                                'profit' => (-$_roi->return_amount + $b_tax_purchase_price) / 100, //利润
                                'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                                'type' => -1, //出库类型
                                'discount' => 0, //配件优惠
                                'item_status' => -1, //领料否
                                'order_status' => $_ro->status, //修理状态
                                'storage_list_id' => $sli_s, //入库批次
                                'storage_supplier_name' => $ss, //入库供货商
                                'data_id' => '', //库房
                                'engineer' => isset($product_engineer) ? $product_engineer->id : '', //修理工
                                'engineer_name' => isset($product_engineer) ? $product_engineer->name : '', //修理工
                                'order_engineer' => $_ro->engineer_id, //结车员
                                'order_engineer_name' => isset($order_engineer) ? $order_engineer->name : '', //接车元
                                'level_1_id' => $part_classification['level_1_id'],
                                'level_1_name' => $part_classification['level_1_name'],
                                'level_2_id' => $part_classification['level_2_id'],
                                'level_2_name' => $part_classification['level_2_name'],
                                'level_3_id' => $part_classification['level_3_id'],
                                'level_3_name' => $part_classification['level_3_name'],
                                'order_info' => $temp_part_classification,
                                'return_type' => 1,
                            );
                            array_push($export_detail, $_export_detail);
                        }
                    }
                }
            }
        }
        return $export_detail;
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-07-19 18:39:24
     * @Description: 未结算的 维修商品出库明细
     */
    public static function temp_get_export_detail($shop_id){
        $export_detail = array();

        $order = Orders::whereIn('status',array(1216,1416,1215))
            ->where('shop_id',$shop_id)
            ->where('place',2)
            ->get();
        foreach ($order as $_order){
            $order_maintenance_item = MaintenanceOutOrder::where('order_id',$_order->order_id)
                ->where('status',2)
                ->get();
            foreach ($order_maintenance_item as $_order_maintenance_item){
               // if($_order_maintenance_item->type==0){
               //     $item_info=SupplyItem::getCacheItemInfo($_order_maintenance_item->item_id);
               // }else{
               //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_order_maintenance_item->item_id);
               // }
               // $part_classification = Report::get_part_classification($item_info->st_classifi_id);
                $item_info = SupplyItem::find($_order_maintenance_item->item_id);
                $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                $maintenance_out_item = MaintenanceOutItem::where('order_out_id',$_order_maintenance_item->id)
                    ->where('status',1)
                    ->get();
                switch ($_order_maintenance_item->product_type) {
                    case 'enterprise':
                        $order_product_item = OrderProductEsItem::find($_order_maintenance_item->product_item_id);
                        break;
                    case 'activity':
                        $order_product_item = OrderProductAcItem::find($_order_maintenance_item->product_item_id);
                        break;
                    case 'promotion':
                        $order_product_item = OrderProductItem::find($_order_maintenance_item->product_item_id);
                        break;
                    case 'base':
                        $order_product_item = OrderProductBaseItem::find($_order_maintenance_item->product_item_id);
                        break;
                    case 'non_standard':
                        $order_product_item = OrderProductNonStandard::find($_order_maintenance_item->product_item_id);
                        break;
                }
                // $item_detail = Report::get_supply_detail($_order_maintenance_item->item_id,$_order_maintenance_item->type);
                foreach ($maintenance_out_item as $_maintenance_out_item){
                    $number=$_maintenance_out_item->number-$_maintenance_out_item->return_numer;
                    $all_amount=($order_product_item->amount/100)*$number;
                    $amount=(($order_product_item->amount*$order_product_item->discount)/100)*$number;
                    $tax_purchase_price=($_maintenance_out_item->tax_purchase_price / 100)*$number;
                    $maintenance_out_list = MaintenanceOutList::where('id',$_maintenance_out_item->list_id)->first();
                    $_export_detail = array(
                        'item_type' => $_order_maintenance_item->type, //耗材类型
                        'item_id' => $item_info->id, //耗材id
                        'brand_name' => $item_info->brand_name, //耗材品牌
                        'series_name' => $item_info->series_name, //耗材系列
                        'model' => $item_info->model, //型号
                        'pack' => $item_info->volume_size, //包装数量
                        'supply_name' => $item_info->name,
                       // 'unit' => $item_info->unit, //售卖单位
                        'order_type' => $_order->order_type, //客户单位
                        'name' => $_order->name, //客户单位
                        'order_id' => $_order->order_id, //派工单号
                        'created_at' => $_order->created_at."", //结算日期
                        'car_no' => $_order->car_no, //车牌号
                        'car_model' => $_order->car_brand_name, //车型
                        'out_date' => $maintenance_out_list->created_at, //出库日期
                        'itme_code' => $_maintenance_out_item->code, //商品编号
                        'item_name' => $_maintenance_out_item->name, //商品名称
                        'item_type' => $_maintenance_out_item->item_type, //商品类型
                        'item_id' => $_maintenance_out_item->item_id, //商品id
                       // 'specification' => $item_info->specification, //耗材规格型号
                       // 'bz_unit' => $item_info->bz_unit, //耗材单位
                        'number' => $number, //数量
                        'amount' => $amount, //金额
                        'all_amount' => $all_amount, //原价金额
                        'tax_purchase_price' => $tax_purchase_price, //成本
                        'profit' => $amount-$tax_purchase_price, //利润
                        'st_classifi_id' => $item_info->supply_classification_id, //商品分类
                        'type' => $maintenance_out_list->type, //出库类型
                        'discount' => $order_product_item->discount, //配件优惠
                        'item_status' => $_maintenance_out_item->status, //领料否
                        'order_status' => $_order->status, //修理状态
                        'storage_list_id' => $_maintenance_out_item->storage_list_id, //入库批次
                        'data_id' => $_maintenance_out_item->data_id, //库房
                        'engineer' => $_order->engineer_id, //修理工
                        'level_1_id' => $part_classification['level_1_id'],
                        'level_1_name' => $part_classification['level_1_name'],
                        'level_2_id' => $part_classification['level_2_id'],
                        'level_2_name' => $part_classification['level_2_name'],
                        'level_3_id' => $part_classification['level_3_id'],
                        'level_3_name' => $part_classification['level_3_name'],
                        //接车员
                        //货位
                        //客户编码
                        //是否收费
                        //部门
                        //商品原编码
                    );
                    array_push($export_detail,$_export_detail);
                }
            }
        }
        return $export_detail;
    }

    //入库查询
    public static function get_storage_detail($begin_date,$end_date,$shop_id){
        $storage_detail = array();
        $begin_date=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));

        //获取入库单
        // $storage_list = MaintenanceStorageList::whereBetween('implement_time',array($begin_date,$end_date))
        //     ->where('shop_id',$shop_id)
        //     ->where('status',1)
        //     ->whereIn('type',array(1,2))
        //     ->orderBy('implement_time','asc')
        //     ->get();
        // $query_time=time();
        // Log::info("入库sql查询用时:".($query_time-$start_time));
        // foreach ($storage_list as $_storage_list){
        //     $storage_item = MaintenanceStorageItem::where('list_id',$_storage_list->id)->where("status",1)->orderBy('implement_time','asc')->get();//耗时5秒
        //     $supplier = MaintenanceSupplier::find($_storage_list->supplier);
        //     $shop_info = MaintenanceShops::find($_storage_list->consuming_id);//上面两个查询语句耗时 4秒
        //     foreach ($storage_item as $_storage_item){
        //         $item_detail = Report::get_supply_detail($_storage_item->item_id,$_storage_item->item_type);
        //         $part_classification = Report::get_part_classification($item_detail->st_classifi_id);
        //         if($_storage_item->tax_purchase_subtotal == 0){
        //             $tax_rate = 0;
        //         }else{
        //             $tax_rate = ($_storage_item->no_tax_purchase_price * $_storage_item->number)/$_storage_item->tax_purchase_subtotal ;
        //         }
        //         $date_info=MainDateBase::find($_storage_item->data_id);
        //         // $_storage_detail = array(
        //         //     'storage_list_id' => $_storage_list->id, //单号
        //         //     'list_type' => '入库',
        //         //     'date' => $_storage_list->date, //日期
        //         //     'supplier' => isset($supplier)?$supplier->name:'', //供货商名称
        //         //     'consuming_id' => isset($shop_info)?$shop_info->id:'', //领用门店id
        //         //     'consuming_name' => isset($shop_info)?$shop_info->name:'', //领用门店名字
        //         //     'code' => $_storage_item->code, //商品编码
        //         //     'name' => $_storage_item->name, //商品名称
        //         //     'specification' => $item_detail->specification, //规格型号
        //         //     'bz_unit' => $item_detail->bz_unit, //单位
        //         //     'number' => $_storage_item->number, //数量
        //         //     'tax_purchase_price' => $_storage_item->tax_purchase_price, //含税进价
        //         //     'tax_purchase_subtotal' => $_storage_item->tax_purchase_subtotal, //含税小计
        //         //     'tax_rate' => $tax_rate,
        //         //     'st_classifi_id' => $item_detail->st_classifi_id, //商品分类
        //         //     'storage_type' => $_storage_list->storage_type, //入库方式
        //         //     'data_id' => $_storage_item->data_id, //库房名称
        //         //     'data_name' => isset($date_info)?$date_info->name:'', //库房名称
        //         //     'no_tax_purchase_price' => $_storage_item->no_tax_purchase_price, //无税进价
        //         //     'no_tax_purchase_subtotal' => $_storage_item->no_tax_purchase_price * $_storage_item->number, //无税小计
        //         //     'pay_type' => $_storage_list->pay_type, //付款方式
        //         //     'type' => $_storage_item->type, //入库类型
        //         //     'storage_list_type' => $_storage_list->type, //入库类型
        //         //     'level_1_id' => $part_classification['level_1_id'],
        //         //     'level_1_name' => $part_classification['level_1_name'],
        //         //     'level_2_id' => $part_classification['level_2_id'],
        //         //     'level_2_name' => $part_classification['level_2_name'],
        //         //     'level_3_id' => $part_classification['level_3_id'],
        //         //     'level_3_name' => $part_classification['level_3_name'],
        //         //     //货位
        //         //     //车型
        //         //     //产地
        //         //     //库房编码
        //         //     //备注
        //         //     //品牌代码
        //         //     //供货商编码
        //         //     //操作员ID
        //         //     //操作员
        //         //     //库管员ID
        //         //     //商品原编码
        //         // );
        //         // array_push($storage_detail,$_storage_detail);
        //     }//耗时10秒
        // }
        //----------------------------------------------------------------------------------------
        //LEFT JOIN (
        //select  0 type,id,st_classifi_id,specification,bz_unit from supply_item union all
        //        select  1 type,id,st_classifi_id,specification,bz_unit from supply_item_non_standard
        //    ) as si on si.type=moi.item_type and si.id=moi.item_id
        //    LEFT JOIN statistical_classification st3 on st3.id=si.st_classifi_id
        //    left join statistical_classification st2 on st3.parent_id=st2.id
        //    left join statistical_classification st1 on st2.parent_id=st1.id
        if($shop_id == 0) {
           //$storage_list = DB::select("SELECT  msi.list_id,msi.type,msi.item_type,msi.item_id,maintenance_storage_list.date,maintenance_storage_list.shop_id,maintenance_storage_list.team,maintenance_storage_list.storage_type ,maintenance_storage_list.type storage_list_type,maintenance_storage_list.pay_type ,maintenance_supplier.name supplier,maintenance_storage_list.cust_id,department.name consuming_name,msi.`code`,msi.`name`,msi.number,msi.tax_purchase_price,msi.tax_purchase_subtotal,msi.data_id,msi.no_tax_purchase_price,maintenance_datebase.name date_name, si.*,st3.id level_3_id,st3.`name` level_3_name,st2.id level_2_id,st2.`name` level_2_name,st1.id level_1_id,st1.`name` level_1_name
            $storage_list = DB::select("SELECT  msi.list_id,msi.type,msi.item_type,msi.item_id,maintenance_storage_list.date,maintenance_storage_list.shop_id,maintenance_storage_list.team,maintenance_storage_list.storage_type ,maintenance_storage_list.type storage_list_type,maintenance_storage_list.pay_type ,maintenance_supplier.name supplier,maintenance_storage_list.cust_id,department.name consuming_name,msi.`code`,msi.`name`,msi.number,msi.tax_purchase_price,msi.tax_purchase_subtotal,msi.data_id,msi.no_tax_purchase_price,maintenance_datebase.name date_name
            FROM
                maintenance_storage_item msi
            LEFT JOIN maintenance_storage_list ON msi.list_id = maintenance_storage_list.id
            LEFT JOIN department ON department.id = maintenance_storage_list.consuming_id
            LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_storage_list.supplier
            LEFT JOIN maintenance_datebase on maintenance_datebase.id=msi.data_id 
            WHERE
                `maintenance_storage_list`.`implement_time` BETWEEN ?
            AND ?
            AND `maintenance_storage_list`.`status` = 1
            AND `msi`.`status` = 1
            AND `maintenance_storage_list`.`type` IN (1, 2)
            ORDER BY
                `maintenance_storage_list`.`implement_time` ASC", array($begin_date, $end_date));
        }else{
            $storage_list = DB::select("SELECT  msi.list_id,msi.type,msi.item_type,msi.item_id,maintenance_storage_list.date,maintenance_storage_list.shop_id,maintenance_storage_list.team,maintenance_storage_list.storage_type ,maintenance_storage_list.type storage_list_type,maintenance_storage_list.pay_type ,maintenance_supplier.name supplier,maintenance_storage_list.cust_id,department.name consuming_name,msi.`code`,msi.`name`,msi.number,msi.tax_purchase_price,msi.tax_purchase_subtotal,msi.data_id,msi.no_tax_purchase_price,maintenance_datebase.name date_name
            FROM
                maintenance_storage_item msi
            LEFT JOIN maintenance_storage_list ON msi.list_id = maintenance_storage_list.id
            LEFT JOIN department ON department.id = maintenance_storage_list.consuming_id
            LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_storage_list.supplier
            LEFT JOIN maintenance_datebase on maintenance_datebase.id=msi.data_id 
            WHERE
                `maintenance_storage_list`.`implement_time` BETWEEN ?
            AND ?
            AND `maintenance_storage_list`.`shop_id` = ?
            AND `maintenance_storage_list`.`status` = 1
            AND `msi`.`status` = 1
            AND `maintenance_storage_list`.`type` IN (1, 2)
            ORDER BY
                `maintenance_storage_list`.`implement_time` ASC", array($begin_date, $end_date, $shop_id));
        }
        //----------------------------------------------------------------------------------------
        foreach ($storage_list as $_storage_list) {
            $_storage_detail = array(
                'storage_list_id' => $_storage_list->list_id, //单号
                'list_type' => '入库',
                'date' => $_storage_list->date, //日期
                'supplier' => $_storage_list->supplier, //供货商名称
                'consuming_id' =>$_storage_list->cust_id, //领用门店id
                'consuming_name' =>$_storage_list->consuming_name, //领用门店名字
                'code' => $_storage_list->code, //商品编码
                'name' => $_storage_list->name, //商品名称
               // 'specification' => $_storage_list->specification, //规格型号
               // 'bz_unit' => $_storage_list->bz_unit, //单位
                'number' => $_storage_list->number, //数量
                'tax_purchase_price' => $_storage_list->tax_purchase_price, //含税进价
                'tax_purchase_subtotal' => $_storage_list->tax_purchase_subtotal, //含税小计
                'tax_rate' => $_storage_list->tax_purchase_price==0?0:($_storage_list->tax_purchase_price * $_storage_list->number)/$_storage_list->tax_purchase_subtotal,
               // 'st_classifi_id' => $_storage_list->st_classifi_id, //商品分类
                'storage_type' => $_storage_list->storage_type, //入库方式
                'data_id' => $_storage_list->data_id, //库房名称
                'data_name' =>$_storage_list->date_name, //库房名称
                'no_tax_purchase_price' => $_storage_list->no_tax_purchase_price, //无税进价
                'no_tax_purchase_subtotal' => $_storage_list->no_tax_purchase_price * $_storage_list->number, //无税小计
                'pay_type' => $_storage_list->pay_type, //付款方式
                'type' => $_storage_list->type, //入库类型
                'item_type' => $_storage_list->item_type, //耗材类型
                'item_id' => $_storage_list->item_id, //耗材id
                'storage_list_type' => $_storage_list->storage_list_type, //入库类型
                'menDianId' => $_storage_list->shop_id,
                'team_id' => $_storage_list->team,
            );
            array_push($storage_detail,$_storage_detail);
        }


        //出库退货
        // $maintenance_out_list = MaintenanceOutList::whereBetween('created_at',array($begin_date,$end_date))
        //     ->where('shop_id',$shop_id)
        //     ->where('status',1)
        //     ->where('type',2)
        //     ->where('out_type',2)
        //     ->orderBy('implement_time','asc')
        //     ->get();
        if($shop_id == 0) {
            $out_list = DB::select("SELECT  moi.list_id,msi.type,moi.item_type,moi.item_id,maintenance_out_list.date,maintenance_out_list.shop_id,maintenance_out_list.team,maintenance_supplier.name supplier,moi.`code`,moi.`name`,moi.number,moi.tax_purchase_price,moi.tax_purchase_subtotal
                    ,msl.storage_type ,msl.type storage_list_type,maintenance_out_list.pay_type 
            ,moi.data_id,moi.no_tax_purchase_price,maintenance_datebase.name date_name 
            FROM
                maintenance_out_item moi
            LEFT JOIN maintenance_out_list ON moi.list_id = maintenance_out_list.id
            LEFT JOIN maintenance_storage_item msi ON msi.id = moi.storage_id
            LEFT JOIN maintenance_storage_list msl ON msl.id = moi.storage_list_id
            LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_out_list.consuming_id
            LEFT JOIN maintenance_datebase on maintenance_datebase.id=moi.data_id
            WHERE
                maintenance_out_list.implement_time BETWEEN ?
            AND ?
            AND `maintenance_out_list`.`status` = 1
            AND `moi`.`status` = 1
            AND `maintenance_out_list`.`type` = 2
            AND `maintenance_out_list`.`out_type` = 2
            ORDER BY
                `maintenance_out_list`.`implement_time` ASC", array($begin_date, $end_date));
        }else{
            $out_list = DB::select("SELECT  moi.list_id,msi.type,moi.item_type,moi.item_id,maintenance_out_list.date,maintenance_out_list.shop_id,maintenance_out_list.team,maintenance_supplier.name supplier,moi.`code`,moi.`name`,moi.number,moi.tax_purchase_price,moi.tax_purchase_subtotal
                    ,msl.storage_type ,msl.type storage_list_type,maintenance_out_list.pay_type 
            ,moi.data_id,moi.no_tax_purchase_price,maintenance_datebase.name date_name 
            FROM
                maintenance_out_item moi
            LEFT JOIN maintenance_out_list ON moi.list_id = maintenance_out_list.id
            LEFT JOIN maintenance_storage_item msi ON msi.id = moi.storage_id
            LEFT JOIN maintenance_storage_list msl ON msl.id = moi.storage_list_id
            LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_out_list.consuming_id
            LEFT JOIN maintenance_datebase on maintenance_datebase.id=moi.data_id
            WHERE
                maintenance_out_list.implement_time BETWEEN ?
            AND ?
            AND `maintenance_out_list`.`shop_id` = ?
            AND `maintenance_out_list`.`status` = 1
            AND `moi`.`status` = 1
            AND `maintenance_out_list`.`type` = 2
            AND `maintenance_out_list`.`out_type` = 2
            ORDER BY
                `maintenance_out_list`.`implement_time` ASC", array($begin_date, $end_date, $shop_id));
        }
        foreach ($out_list as $_storage_list) {
            $_storage_detail = array(
                'storage_list_id' => $_storage_list->list_id, //单号
                'list_type' => '退货',
                'date' => $_storage_list->date, //日期
                'supplier' => $_storage_list->supplier, //供货商名称
                'consuming_id' =>"", //领用门店id
                'consuming_name' =>"", //领用门店名字
                'code' => $_storage_list->code, //商品编码
                'name' => $_storage_list->name, //商品名称
               // 'specification' => $_storage_list->specification, //规格型号
               // 'bz_unit' => $_storage_list->bz_unit, //单位
                'number' => $_storage_list->number, //数量
                'tax_purchase_price' => $_storage_list->tax_purchase_price, //含税进价
                'tax_purchase_subtotal' => $_storage_list->tax_purchase_subtotal, //含税小计
                'tax_rate' => $_storage_list->tax_purchase_price==0?0:($_storage_list->tax_purchase_price * $_storage_list->number)/$_storage_list->tax_purchase_subtotal,
               // 'st_classifi_id' => $_storage_list->st_classifi_id, //商品分类
                'storage_type' => $_storage_list->storage_type, //入库方式
                'data_id' => $_storage_list->data_id, //库房名称
                'data_name' =>$_storage_list->date_name, //库房名称
                'no_tax_purchase_price' => $_storage_list->no_tax_purchase_price, //无税进价
                'no_tax_purchase_subtotal' => $_storage_list->no_tax_purchase_price * $_storage_list->number, //无税小计
                'pay_type' => $_storage_list->pay_type, //付款方式
                'type' => $_storage_list->type, //入库类型
                'item_type' => $_storage_list->item_type, //耗材类型
                'item_id' => $_storage_list->item_id, //耗材id
                'storage_list_type' => $_storage_list->storage_list_type, //入库类型
                'menDianId' => $_storage_list->shop_id,
                'team_id' => $_storage_list->team,
            );
            array_push($storage_detail,$_storage_detail);
        }
        // foreach ($maintenance_out_list as $_maintenance_out_list){
        //     $supplier = MaintenanceSupplier::where('id',$_maintenance_out_list->consuming_id)->first();
        //     $maintenance_out_item = MaintenanceOutItem::where('list_id',$_maintenance_out_list->id)
        //         ->where('status',1)
        //         ->get();
        //     foreach ($maintenance_out_item as $_maintenance_out_item){
        //         $item_detail = Report::get_supply_detail($_maintenance_out_item->item_id,$_maintenance_out_item->item_type);
        //         $part_classification = Report::get_part_classification($item_detail->st_classifi_id);
        //         $_storage_list = MaintenanceStorageList::find($_maintenance_out_item->storage_list_id);
        //         if($_maintenance_out_item->tax_purchase_subtotal == 0){
        //             $tax_rate = 0;
        //         }else{
        //             $tax_rate = ($_maintenance_out_item->no_tax_purchase_price * $_maintenance_out_item->number)/$_maintenance_out_item->tax_purchase_subtotal ;
        //         }
        //         $date_info=MainDateBase::find($_maintenance_out_item->data_id);
        //         $_storage_detail = array(
        //             'storage_list_id' => $_maintenance_out_list->id, //单号
        //             'list_type' => '退货',
        //             'date' => $_maintenance_out_list->date, //日期
        //             'supplier' => isset($supplier)?$supplier->name:'', //供货商名称
        //             'consuming_id' => '', //领用门店id
        //             'consuming_name' => '', //领用门店名字
        //             'code' => $_maintenance_out_item->code, //商品编码
        //             'name' => $_maintenance_out_item->name, //商品名称
        //             'specification' => $item_detail->specification, //规格型号
        //             'bz_unit' => $item_detail->bz_unit, //单位
        //             'number' => $_maintenance_out_item->number * -1, //数量
        //             'tax_purchase_price' => $_maintenance_out_item->tax_purchase_price, //含税进价
        //             'tax_purchase_subtotal' => $_maintenance_out_item->tax_purchase_subtotal * -1, //含税小计
        //             'tax_rate' => $tax_rate,
        //             'st_classifi_id' => $item_detail->st_classifi_id, //商品分类
        //             'storage_type' => $_storage_list->storage_type, //入库方式
        //             'data_id' => $_maintenance_out_item->data_id, //库房名称
        //             'data_name' => isset($date_info)?$date_info->name:'', //库房名称
        //             'no_tax_purchase_price' => $_maintenance_out_item->no_tax_purchase_price, //无税进价
        //             'no_tax_purchase_subtotal' => $_maintenance_out_item->no_tax_purchase_price * $_maintenance_out_item->number, //无税小计
        //             'pay_type' => $_maintenance_out_list->pay_type, //付款方式
        //             'type' => 2, //入库类型
        //             'storage_list_type' => $_storage_list->type, //入库类型
        //             'level_1_id' => $part_classification['level_1_id'],
        //             'level_1_name' => $part_classification['level_1_name'],
        //             'level_2_id' => $part_classification['level_2_id'],
        //             'level_2_name' => $part_classification['level_2_name'],
        //             'level_3_id' => $part_classification['level_3_id'],
        //             'level_3_name' => $part_classification['level_3_name'],
        //             //货位
        //             //车型
        //             //产地
        //             //库房编码
        //             //备注
        //             //品牌代码
        //             //供货商编码
        //             //操作员ID
        //             //操作员
        //             //库管员ID
        //             //商品原编码
        //         );
        //         array_push($storage_detail,$_storage_detail);
        //     }
        // }
        return $storage_detail;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-06-29 09:46:23
     * @Description: Description
     */
    public static function get_out_detail($begin_date,$end_date,$shop_id){
        $storage_detail = array();

        $begin_date=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));

        // 获取入库单
        $storage_list=MaintenanceOutList::leftJoin("sc_user",'sc_user.id','=','maintenance_out_list.cust_id')->
        leftJoin("department",'department.id','=','maintenance_out_list.consuming_id')->
        leftJoin("maintenance_supplier",'maintenance_supplier.id','=','maintenance_out_list.consuming_id')->where("maintenance_out_list.shop_id",$shop_id)->where("maintenance_out_list.status",1)->whereBetween('maintenance_out_list.implement_time',array($begin_date,$end_date))->select("maintenance_out_list.id","maintenance_out_list.type","maintenance_out_list.implement_time","maintenance_out_list.out_type","maintenance_out_list.methed","maintenance_out_list.status","maintenance_out_list.consuming_type","maintenance_out_list.desc","maintenance_supplier.name as suppliername","sc_user.real_name","department.name as shop_name","maintenance_out_list.shop_type","maintenance_out_list.user_type","maintenance_out_list.consuming_id")->orderBy("maintenance_out_list.implement_time","asc")->get();
        foreach ($storage_list as $_storage_list){
            $storage_item=MaintenanceOutItem::leftJoin("sc_user",'sc_user.id','=','maintenance_out_item.own_id')->
            leftJoin("maintenance_datebase",'maintenance_datebase.id','=','maintenance_out_item.data_id')->
            leftJoin("maintenance_storage_list",'maintenance_storage_list.id','=','maintenance_out_item.storage_list_id')->
            where("maintenance_out_item.list_id",$_storage_list->id)->where("maintenance_out_item.status",1)->select("maintenance_out_item.*","sc_user.real_name","maintenance_datebase.name as data_name","maintenance_storage_list.pay_type")->orderBy("maintenance_out_item.implement_time","asc")->get();

            foreach ($storage_item as $_storage_item){
               // if($_storage_item->item_type==0){
               //     $item_info=SupplyItem::getCacheItemInfo($_storage_item->item_id);
               // }else{
               //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_storage_item->item_id);
               // }
                $item_info = SupplyItem::find($_storage_item->item_id);
                $part_classification = Report::get_part_classification($item_info->supply_classification_id);
                $order_info=array();
                if($_storage_list->type==3){//说明是订单出库
                    $moo=MaintenanceOutOrder::find($_storage_item->order_out_id);
                    if($moo){
                        $or=Orders::find($moo->order_id);
                        switch ($moo->product_type) {
                            case 'enterprise':
                                $order_product_item = OrderProductEsItem::find($moo->product_item_id);
                                $opep=OrderProductEsPackage::find($order_product_item->order_product_package_id);
                                $product=ProductPackage::find($opep->package_id);
                                $st=$product->classify;
                                break;
                            case 'activity':
                                $order_product_item = OrderProductAcItem::find($moo->product_item_id);
                                $opep=OrderProductAcPackage::find($order_product_item->order_product_package_id);
                                $product=ProductPackage::find($opep->package_id);
                                $st=$product->classify;
                                break;
                            case 'promotion':
                                $order_product_item = OrderProductItem::find($moo->product_item_id);
                                $opep=OrderProductPackage::find($order_product_item->order_product_package_id);
                                $product=ProductPackage::find($opep->package_id);
                                $st=$product->classify;
                                break;
                            case 'base':
                                $order_product_item = OrderProductBaseItem::find($moo->product_item_id);
                                $op=OrderProduct::find($order_product_item->order_product_package_id);
                                $product=ProductPackage::find($op->product_id);
                                $st=$product->classify;
                                break;
                            case 'non_standard':
                                $order_product_item = OrderProductNonStandard::find($moo->product_item_id);
                                $op=OrderProduct::find($order_product_item->order_product_id);
                                $s=Service::find($op->product_id);
                                $st=$s->classify;
                                break;
                            case 'service_card':
                                $order_product_item = OrderProductScItem::find($moo->product_item_id);
                                $op=OrderProduct::find($order_product_item->order_product_id);
                                $s=ProductPackage::find($op->product_id);
                                $st=$s->classify;
                                break;
                        }
                        $temp_part_classification = Report::get_service_classification($st);
                        $temp_part_classification["order_id"]=$or->order_id;
                        $temp_part_classification["status"]=$or->status;
                        $order_info=$temp_part_classification;
                    }

                }
                $shop_name="";
                if($_storage_list->consuming_type==1){
                    if($_storage_list->methed==1){
                        if($_storage_list->type==1){
                            $department=Department::find($_storage_list->consuming_id);
                            $shop_name=$department->name;
                        }else{
                            $ms=MaintenanceShops::find($_storage_list->consuming_id);
                            $shop_name=$ms->name;
                        }
                    }elseif($_storage_list->methed==3){
                        if($_storage_list->shop_type==1){
                            if($_storage_list->user_type==1){
                                $sc=SCUser::find($_storage_list->consuming_id);
                                $shop_name=$sc->real_name;
                            }else{
                                $me=MaintenanceEngineers::find($_storage_list->consuming_id);
                                $shop_name=$me->name;
                            }
                        }else{
                            $department=Department::find($_storage_list->consuming_id);
                            $shop_name=$department->name;
                        }
                    }else{
                        $ms=MaintenanceShops::find($_storage_list->consuming_id);
                        $shop_name=$ms->name;
                    }
                }

                $_storage_detail = array(
                    'out_list_id' => $_storage_list->id, //单号
                    'date' =>date("Y-m-d H:i:s",strtotime($_storage_list->implement_time)+Config::get("config.report_time_diff")) , //日期
                    'supplier_name' => $_storage_list->consuming_type==1?"":$_storage_list->suppliername, //供货商名称
                    'shop_name' => $shop_name, //供货商名称
                    'out_type' => $_storage_list->out_type, //出库方式 1内部领用 2退货
                    'methed' => $_storage_list->methed, //领用方式 1领用 2 调出
                    'type' => $_storage_list->type, //1 内部领用 2退货出库 3订单出库
                    'desc' => $_storage_list->desc, //备注
                    'storage_list_id' => $_storage_item->storage_list_id, //入库批次
                    'storage_id' => $_storage_item->storage_id, //入库记录id
                    'tax_purchase_price' => $_storage_item->tax_purchase_price, //含税进价
                    'number' => $_storage_item->number,//出库数量
                    'return_numer' => $_storage_item->return_numer, //退库数量
                    'location' => $_storage_item->location, //货位
                    'data_id' => $_storage_item->data_id, //库房id
                    'data_name' => $_storage_item->data_name, //库房名字
                    'sale_amount' => $_storage_item->sale_amount, //售价
                    'item_id' => $_storage_item->item_id, //耗材id
                    'item_type' => $_storage_item->item_type, //耗材类型
                    'own_id' => $_storage_item->own_id, //操作员
                    'own_real_name' => $_storage_item->real_name, //操作员名字
                    'brand_name' => $item_info->brand_name, //耗材品牌
                    'series_name' => $item_info->series_name, //耗材系列
                    'model' => $item_info->model, //型号
                    'name' => $item_info->name, //名称
                   // 'specification' => $item_info->specification, //耗材规格型号
                    'pack' => $item_info->volume_size, //包装数量
                   // 'unit' => $item_info->unit, //售卖单位
                   // 'bz_unit' => $item_info->bz_unit, //耗材单位
                    'code' => $item_info->code, //商品编码
                    'level_1_id' => $part_classification['level_1_id'],
                    'level_1_name' => $part_classification['level_1_name'],
                    'level_2_id' => $part_classification['level_2_id'],
                    'level_2_name' => $part_classification['level_2_name'],
                    'level_3_id' => $part_classification['level_3_id'],
                    'level_3_name' => $part_classification['level_3_name'],
                    'order_info'=>isset($order_info)?$order_info:array(),
                    'team_id'=>$_storage_item->team_id,
                    'pay_type'=>$_storage_item->pay_type,// 18s2新增 历史的记录没有该字段
                    //货位
                    //车型
                    //产地
                    //库房编码
                    //备注
                    //品牌代码
                    //供货商编码
                    //操作员ID
                    //操作员
                    //库管员ID
                    //商品原编码
                );
                array_push($storage_detail,$_storage_detail);
            }
        }
        return $storage_detail;
    }
    /**
     * @Author:      lyt
     * @DateTime:    2016-10-31 10:56:51
     * @Description: 用于替代上一个
     */
    public static function new_get_out_detail($begin_date,$end_date,$shop_id){
        $storage_detail = array();

        $begin_date=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));
        //新的 出库查询
        //第一步获取这些出库单的 订单信息 可以与下面混合查询
        $start=time();
        // $order=DB::statement();
        $sql="SELECT moi.id,orders.order_id,orders.`status`,sc1.`level` as level_1_id, sc1.`name` as level_1_name,
                sc2.`level` as level_2_id, sc2.`name` as level_2_name,sc3.`level` as level_3_id, sc3.`name` as level_3_name FROM
              maintenance_out_item moi
            LEFT JOIN maintenance_out_list mol ON moi.list_id = mol.id
            LEFT JOIN maintenance_out_order moo ON moo.id = moi.order_out_id
            LEFT JOIN orders on orders.order_id=moo.order_id
            LEFT JOIN
            (
                select 'base' type,opbi.id,op.product_id,pp.classify from order_product_base_item opbi LEFT JOIN order_product op on op.id=opbi.order_product_package_id LEFT JOIN product_package pp on pp.id=op.product_id 
                union all
                select 'non_standard' type,opns.id,op.product_id,s.classify FROM order_product_non_standard opns LEFT JOIN order_product op on op.id=opns.order_product_id LEFT JOIN service s on s.id=op.product_id 
                union all
                select 'enterprise' type,opei.id,opep.package_id as product_id,pp.classify FROM order_product_esitem opei LEFT JOIN order_product_espackage opep on opei.order_product_package_id=opep.id LEFT JOIN product_package pp on pp.id=opep.package_id 
                union all
                select 'activity' type,opai.id,opap.package_id as product_id,pp.classify FROM order_product_acitem opai LEFT JOIN order_product_acpackage opap on opai.order_product_package_id=opap.id LEFT JOIN product_package pp on pp.id=opap.package_id 
            ) 
            as temp on moo.product_item_id=temp.id and moo.product_type=temp.type
            LEFT JOIN service_classification sc3 on sc3.id=temp.classify
            left join service_classification sc2 on sc3.parent_id=sc2.id
            left join service_classification sc1 on sc2.parent_id=sc1.id
            WHERE
                mol.implement_time BETWEEN '".$begin_date."'
            AND '".$end_date."'
            AND mol.`type` = 3
            AND mol.`shop_id` = ".$shop_id."
            AND mol.`status` = 1
            AND `moi`.`status` = 1";
        // $order=DB::select("SELECT moi.id,orders.order_id,orders.`status`,sc1.`level` as level_1_id, sc1.`name` as level_1_name,
        //         sc2.`level` as level_2_id, sc2.`name` as level_2_name,sc3.`level` as level_3_id, sc3.`name` as level_3_name FROM
        //       maintenance_out_item moi
        //     LEFT JOIN maintenance_out_list mol ON moi.list_id = mol.id
        //     LEFT JOIN maintenance_out_order moo ON moo.id = moi.order_out_id
        //     LEFT JOIN orders on orders.order_id=moo.order_id
        //     LEFT JOIN
        //     (
        //         select 'base' type,opbi.id,op.product_id,pp.classify from order_product_base_item opbi LEFT JOIN order_product op on op.id=opbi.order_product_package_id LEFT JOIN product_package pp on pp.id=op.product_id
        //         union all
        //         select 'non_standard' type,opns.id,op.product_id,s.classify FROM order_product_non_standard opns LEFT JOIN order_product op on op.id=opns.order_product_id LEFT JOIN service s on s.id=op.product_id
        //         union all
        //         select 'enterprise' type,opei.id,opep.package_id as product_id,pp.classify FROM order_product_esitem opei LEFT JOIN order_product_espackage opep on opei.order_product_package_id=opep.id LEFT JOIN product_package pp on pp.id=opep.package_id
        //     )
        //     as temp on moo.product_item_id=temp.id and moo.product_type=temp.type
        //     LEFT JOIN service_classification sc3 on sc3.id=temp.classify
        //     left join service_classification sc2 on sc3.parent_id=sc2.id
        //     left join service_classification sc1 on sc2.parent_id=sc1.id
        //     WHERE
        //         mol.implement_time BETWEEN ?
        //     AND ?
        //     AND mol.`type` = 3
        //     AND mol.`shop_id` = ?
        //     AND mol.`status` = 1
        //     AND `moi`.`status` = 1",array($begin_date,$end_date,$shop_id));
        // DB::getReadPdo()->query($sql);
        // Log::info(DB::getQueryLog());
        $order=DB::select($sql);
        $end_time=time();
        Log::info("查询用时".($end_time-$start));
        $order_array=array();
        foreach ($order as $_o) {
            $order_array[$_o->id]=$_o;
        }
        $two_time=time();
        Log::info("组结构".($two_time-$end_time));

        $out_list=DB::select("SELECT  moi.id as moi_id,maintenance_out_list.implement_time,maintenance_out_list.out_type,maintenance_out_list.type,maintenance_out_list.methed,maintenance_out_list.`desc`,moi.list_id,maintenance_out_list.date,maintenance_out_list.consuming_type,maintenance_out_list.consuming_id,maintenance_supplier.name supplier,department.`name` as shop_name,
                moi.`code`,moi.`name`,moi.number,moi.tax_purchase_price,moi.tax_purchase_subtotal,moi.storage_list_id,moi.storage_id,moi.return_numer,moi.location,moi.sale_amount,moi.own_id
                    ,maintenance_out_list.pay_type 
            ,moi.data_id,moi.no_tax_purchase_price,maintenance_datebase.name date_name, sc_user.real_name,
                si.*,st3.id level_3_id,st3.`name` level_3_name,st2.id level_2_id,st2.`name` level_2_name,st1.id level_1_id,st1.`name` level_1_name
            FROM
                maintenance_out_item moi
            LEFT JOIN maintenance_out_list ON moi.list_id = maintenance_out_list.id
            LEFT JOIN maintenance_supplier ON maintenance_supplier.id = maintenance_out_list.consuming_id
            LEFT JOIN department ON department.id = maintenance_out_list.consuming_id
            LEFT JOIN maintenance_datebase on maintenance_datebase.id=moi.data_id
            LEFT JOIN sc_user on moi.own_id=sc_user.id
            LEFT JOIN (
                select  0 item_type,supply_item.id as item_id,supply_item.st_classifi_id,specification,bz_unit,code,model,sale_unit as unit,pack,supply_series.`name` as series_name,supply_brand.`brand_name` from supply_item LEFT JOIN supply_series on supply_item.series_id=supply_series.id LEFT JOIN supply_brand on supply_brand.id=supply_item.brand_id
                union all
                select  1 item_type,id as item_id,st_classifi_id,specification,bz_unit,code,model,unit,pack_unit as pack,series_name,brand_name from supply_item_non_standard where shop_id=?
            ) as si on si.item_type=moi.item_type and si.item_id=moi.item_id
            LEFT JOIN statistical_classification st3 on st3.id=si.st_classifi_id
            left join statistical_classification st2 on st3.parent_id=st2.id
            left join statistical_classification st1 on st2.parent_id=st1.id
            WHERE
                maintenance_out_list.implement_time BETWEEN ?
            AND ?
            AND `maintenance_out_list`.`shop_id` = ?
            AND `maintenance_out_list`.`status` = 1
            AND `moi`.`status` = 1
            ORDER BY
                `maintenance_out_list`.`implement_time` ASC",array($shop_id,$begin_date,$end_date,$shop_id));
        $query_time=time();
        Log::info("主查询".($query_time-$two_time));
        foreach ($out_list as $_out_list) {
            $_storage_detail = array(
                'out_list_id' => $_out_list->list_id, //单号
                'date' =>date("Y-m-d H:i:s",strtotime($_out_list->implement_time)+Config::get("config.report_time_diff")) , //日期
                'supplier_name' => $_out_list->consuming_type==1?"":$_out_list->supplier, //供货商名称
                'shop_name' => $_out_list->consuming_type==1?$_out_list->shop_name:"", //供货商名称
                'out_type' => $_out_list->out_type, //出库方式 1内部领用 2退货
                'methed' => $_out_list->methed, //领用方式 1领用 2 调出
                'type' => $_out_list->type, //1 内部领用 2退货出库 3订单出库
                'desc' => $_out_list->desc, //备注
                'storage_list_id' => $_out_list->storage_list_id, //入库批次
                'storage_id' => $_out_list->storage_id, //入库记录id
                'tax_purchase_price' => $_out_list->tax_purchase_price, //含税进价
                'number' => $_out_list->number,//出库数量
                'return_numer' => $_out_list->return_numer, //退库数量
                'location' => $_out_list->location, //货位
                'data_id' => $_out_list->data_id, //库房id
                'data_name' => $_out_list->date_name, //库房名字
                'sale_amount' => $_out_list->sale_amount, //售价
                'item_id' => $_out_list->item_id, //耗材id
                'item_type' => $_out_list->item_type, //耗材类型
                'own_id' => $_out_list->own_id, //操作员
                'own_real_name' => $_out_list->real_name, //操作员名字
                'brand_name' => $_out_list->brand_name, //耗材品牌
                'series_name' => $_out_list->series_name, //耗材系列
                'model' => $_out_list->model, //型号
                'specification' => $_out_list->specification, //耗材规格型号
                'pack' => $_out_list->pack, //包装数量
                'unit' => $_out_list->unit, //售卖单位
                'bz_unit' => $_out_list->bz_unit, //耗材单位
                'code' => $_out_list->code, //商品编码
                'level_1_id' => $_out_list->level_1_id,
                'level_1_name' => $_out_list->level_1_name,
                'level_2_id' => $_out_list->level_2_id,
                'level_2_name' => $_out_list->level_2_name,
                'level_3_id' => $_out_list->level_3_id,
                'level_3_name' => $_out_list->level_3_name,
                'order_info'=>isset($order_array[$_out_list->moi_id])?$order_array[$_out_list->moi_id]:array(),
            );
            array_push($storage_detail,$_storage_detail);
        }
        Log::info("主结构".(time()-$query_time));
        return $storage_detail;
    }

    /*获取配件的规格和型号*/
    public static function get_supply_detail($item_id,$item_type){
//        if($item_type == 0){ //标准
//            $detail = SupplyItem::where('id',$item_id)->first();
//        }elseif($item_type == 1){ //非标准
//            $detail = SupplyItemNonStandard::where('id',$item_id)->first();
//        }
        $result = SupplyItem::find($item_id);
        return $result;
    }

    /*获取服务的基础信息*/
    public static function get_service_detail($id,$type){
        if($type == 'base'){
            $detail = ProductPackage::where('id',$id)->first();
        }elseif($type == 'non_standard'){
            $detail = Service::where('id',$id)->first();
        }elseif($type == 'service_card'){
            $detail = ProductPackage::where('id',$id)->first();
        }
        return $detail;
    }

    /*获取服务的三级分类*/
    public static function get_service_classification($id){
        if(Cache::has('service_classification_'.$id)){//如果有缓存的 非标准耗材
            return Cache::get('service_classification_'.$id);
        }else{
            $level_3 = ServiceClassification::where('id',$id)->first();
            $level_2 = ServiceClassification::where('id',$level_3->parent_id)->first();
            $level_1 = ServiceClassification::where('id',$level_2->parent_id)->first();
            $service_classification = array(
                'level_1_id' => $level_1->id,
                'level_1_name' => $level_1->name,
                'level_2_id' => $level_2->id,
                'level_2_name' => $level_2->name,
                'level_3_id' => $level_3->id,
                'level_3_name' => $level_3->name,
            );
            Cache::add('service_classification_'.$id,$service_classification,10);
            return $service_classification;
        }
    }

    /*获取配件的三级分类*/
    public static function get_part_classification($id){
        if(Cache::has('statistical_classification_'.$id)){//如果有缓存的 非标准耗材
            return Cache::get('statistical_classification_'.$id);
        }else{
            $part_classification = [
                'level_3_id'=>'',
                'level_3_name'=>'',
                'level_2_id'=>'',
                'level_2_name'=>'',
                'level_1_id'=>'',
                'level_1_name'=>'',
            ];
            $level_3 = SupplyClassification::where('id',$id)->first();
            if($level_3){
                $part_classification['level_3_id'] = $level_3->id;
                $part_classification['level_3_name'] = $level_3->name;
                $level_2 = SupplyClassification::where('id',$level_3->parent_id)->first();
                if($level_2) {
                    $part_classification['level_2_id'] = $level_2->id;
                    $part_classification['level_2_name'] = $level_2->name;
                    $level_1 = SupplyClassification::where('id',$level_2->parent_id)->first();
                    if($level_1) {
                        $part_classification['level_1_id'] = $level_1->id;
                        $part_classification['level_1_name'] = $level_1->name;
                    }
                }
            }
            Cache::add('statistical_classification_'.$id,$part_classification,10);
            return $part_classification;
        }
    }

    /*获取配件的三级分类*/
    public static function get_supply_items($_ro,$_roi,$item,$type_arr,$order_product_detail){
        if(!is_null($item)) {
            foreach ($item as $_item) {
                $supply_name = $service_name = "";
                $return_amount=0;
                if ($_item->type == 1) {//peijian
//                    if ($_item->item_type == 0) {//说明是标准
//                        $item_info = SupplyItem::getCacheItemInfo($_item->item_id);
//                    } else {
//                        $item_info = SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
//                    }
//                    $supply_name = $item_info->brand_name . " " . $item_info->series_name . " " . $item_info->model . " " . $item_info->code . " " . $item_info->specification;
                    $item_info = SupplyItem::find($_item->item_id);
                    if($item_info) {
                        $supply_name = $item_info->name;
                    }
                    $return_amount=$_item->counter_amount*$_item->num;
                } else {
                    $service_name = $_item->item_name;
                    $return_amount=$_item->counter_amount;
                }
                $_order_product_detail = array(
                    'return_order_id' => $_ro->id,
                    'order_id' => $_ro->order_id,
                    'order_type_name' => $type_arr[$_roi->type],
                    'order_type' => $_roi->type,
                    'car_no' => $_ro->car_no,
                    'supply_name' => $supply_name,
                    'service_name' => $service_name,
                    'retunr_number' => $_item->num,
                    'retunr_amount' => $return_amount,
                    'implement_time' => $_ro->implement_time,
                    'created_name' => $_ro->created_name,
                    'approver_name' => $_ro->approver_name,
                    'reason' => $_ro->reason,
                );
                array_push($order_product_detail,$_order_product_detail);
            }
        }
        return $order_product_detail;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-05-31 10:02:22
     * @Description: 传入开始和结束时间获取 使用的 耗材和
     */
    public static function get_supply_byst($begin_date,$end_date,$shop_id){}

    /*获取技师信息*/
    public static function get_engineer_info($id){
        return MaintenanceEngineers::where('id',$id)->first();
    }
    /**
     * @Author:zhanghu
     * @DateTime: 2017-02-11 13:52
     * @Description:计算所有门店当日营业数据（监控数据）
     */
    public static function get_shop_control_data($date,$shop_id){
        $begin_date = date('Y-m-d 00:00:00',strtotime($date));
        $end_date = date('Y-m-d 23:59:59',strtotime($date));
        $begin_date_37_31 = date("Y-m-d H:i:s",strtotime($date." 00:00:00") - 37 * 24 * 60 * 60);
        $end_date_37_31 = date("Y-m-d H:i:s",strtotime($date." 23:59:59") - 31 * 24 * 60 * 60);
        $begin_date_60 = date("Y-m-d H:i:s",strtotime($date." 00:00:00") - 60 * 24 * 60 * 60);
        $end_date_60 = $end_date;
        $begin_date_7 = date("Y-m-d H:i:s",strtotime($date." 00:00:00") - 7 * 24 * 60 * 60);
        $end_date_7 = $end_date;
        $payed_array = array(1018,1017,1217,1317,1417);
        //当天订单
        $order = Orders::whereIn('status',$payed_array)//完结状态的订单
        ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)//1.上门服务，2.到店服务
            ->get();
        $export_detail = array(
            'order_count' => $order->count(),//交易台次     已结算订单数量
            'order_payable_amount' => 0,//订单实收总计 分    已结算订单金额
            'order_fitting_amount' => 0,//配件成本 分        已经结算订单中耗材成本
            'order_gross_profit' => 0,//毛利 分             实收-配件成本=毛利
            'order_gross_profit_rate' => 0,//毛利率 %       毛利/实收
            'order_average' => 0,//单均价 分                订单实收/交易台次
            'order_new_cars' => 0,//新增车辆                第一次下单车辆数量
            'order_add_cars' => 0,//累积车辆                累积计算在门店进行服务车辆数，以单一车牌号为准
            'order_rePurchase_rate' => 0,//60日复购率 %     60天内，同一车辆进行2次以上订单的车辆数/累积车辆数
            'order_keep_rate' => 0,//30日留存率 %           向前推37天，37-31天内新增车辆，在30-当天有过交际行为的车辆/累积车辆数
            'order_stay_sum' => 0,//7日平均在场时长 分/单   记录每个订单，从服务开始到订单结算所用时间，时间转为分钟。以当天为准，
            //计算7天内所有订单所用时间时长/7天内订单数
            'order_count_pur_station' => 0,//单工位台次      服务台次/所使用工位数量
            'order_amount_pur_station' => 0,//单工位产值 分  订单实收/所使用工位数量
            'order_count_pur_man' => 0,//人均台次
            'order_amount_pur_man' => 0,//人均产值 分
        );
        //当天订单
        $cars_array = Array();
        foreach ($order as $_order){
            $export_detail['order_payable_amount'] += $_order->payable_amount;

            //查找领料 或者退货的  因为 存在结算后退货的情况
            $maintenance_out_order = MaintenanceOutOrder::where('order_id',$_order->order_id)
                ->whereIn('status',array(2,3))
                ->get();
            foreach ($maintenance_out_order as $_maintenance_out_order){
                //结算后退货的数量
                $before_return=MaintenanceReturnOrder::where("source",'return_order')
                    ->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->sum("number");
                if($before_return==0&&$_maintenance_out_order->status == 3){//如果是 这个出库申请是结算前 退货完成的  不统计
                    continue;
                }
                //查看 该出库单 结算后退库 多少
                $before_return=MaintenanceReturnOrder::where("source",'return_order')
                    ->where("out_order_id",$_maintenance_out_order->id)->where("status",1)->get();
                $before_return_array=array();//出库批次为键 退了多少为值的 数组
                foreach ($before_return as $_br) {
                    $temp_msi=MaintenanceStorageItem::where("return_order_id",$_br->id)->where("status",1)->get();
                    // if($temp_msi){
                    foreach ($temp_msi as $_temp_msi) {
                        // $before_return_array[$temp_msi->out_id]=$temp_msi->number;
                        if(isset($before_return_array[$_temp_msi->out_id])){
                            $before_return_array[$_temp_msi->out_id] =
                                $before_return_array[$_temp_msi->out_id] + $_temp_msi->number;
                        }else{
                            $before_return_array[$_temp_msi->out_id]=$_temp_msi->number;
                        }
                    }
                }


                $maintenance_out_item = MaintenanceOutItem::where('order_out_id',$_maintenance_out_order->id)
                    // ->where('data_id',$data_id)
                    ->where('status',1)
                    ->get();
                foreach ($maintenance_out_item as $_maintenance_out_item){
                    $e_number=
                        isset($before_return_array[$_maintenance_out_item->id])?
                            $before_return_array[$_maintenance_out_item->id]:0;//该批次的结算后退货 多少个
                    $export_detail['order_fitting_amount'] +=
                        ($_maintenance_out_item->number - $_maintenance_out_item->return_numer + $e_number) *
                        $_maintenance_out_item->tax_purchase_price;
                }
            }
            $car_no = $_order->car_no;
            $pay_time = $_order->pay_time;
            $all_orders = Orders::whereIn('status',$payed_array)
                ->where('pay_time','<',$pay_time)
                ->where('shop_id',$shop_id)
                ->where('car_no',$car_no)
                ->where('place',2)//1.上门服务，2.到店服务
                ->count();
            if($all_orders == 0){
                $export_detail['order_new_cars'] += 1;
            }
            $cars_array[] = $car_no;
        }
        $cars_array = array_flip(array_flip($cars_array));
        //37-31订单
        $order_2 = Orders::whereIn('status',$payed_array)//完结状态的订单
        ->whereBetween('pay_time',array($begin_date_37_31,$end_date_37_31))
            ->where('shop_id',$shop_id)
            ->where('place',2)//1.上门服务，2.到店服务
            ->get();
        $cars_array_2 = array();
        foreach ($order_2 as $_order_2){
            $car_no_2 = $_order_2->car_no;
            $pay_time_2 = $_order_2->pay_time;
            $all_orders = Orders::whereIn('status',$payed_array)
                ->where('pay_time','<',$pay_time_2)
                ->where('shop_id',$shop_id)
                ->where('car_no',$car_no_2)
                ->where('place',2)//1.上门服务，2.到店服务
                ->count();
            if($all_orders == 0){
                $cars_array_2[] = $car_no_2;
            }
        }
        $cars_array_2 = array_flip(array_flip($cars_array_2));
        $come_again_count = 0;
        foreach ($cars_array_2 as $car_a) {
            if(in_array($car_a,$cars_array)){
                $come_again_count += 1;
            }
        }
        //60 内订单
        $order_3 = Orders::whereIn('status',$payed_array)//完结状态的订单
        ->whereBetween('pay_time',array($begin_date_60,$end_date_60))
            ->where('shop_id',$shop_id)
            ->where('place',2)//1.上门服务，2.到店服务
            ->get();
        $all_cars_60 = array();
        $all_cars_again_60 = array();
        foreach ($order_3 as $_order_3){
            $car_no_3 = $_order_3->car_no;
            if(!in_array($car_no_3,$all_cars_60)){
                $all_cars_60[] = $car_no_3;
            }else{
                $all_cars_again_60[] = $car_no_3;
            }
        }
        $all_cars_again_60 = array_flip(array_flip($all_cars_again_60));
        //7 内订单
        $order_4 = Orders::whereIn('status',$payed_array)//完结状态的订单
        ->whereBetween('pay_time',array($begin_date_7,$end_date_7))
            ->where('shop_id',$shop_id)
            ->where('place',2)//1.上门服务，2.到店服务
            ->get();
        $orders_count_7 = 0;
        $orders_time_long_7 = 0;
        foreach($order_4 as $_order_4){
            $orders_count_7 += 1;
            $orders_time_long_7 += (strtotime($_order_4->pay_time) - strtotime($_order_4->start_time));
        }
        $shop_stations_count = MaintenanceStation::where('status',1)->where('shop_id',$shop_id)->count();
        $shop_users_count = SCUser::where('status',1)->where('shop_id',$shop_id)->count();//门店人员
        $shop_engineers_count = MaintenanceEngineers::where('status',1)->where('type','shop')->where('type_id',$shop_id)->count();//门店技师
        $shop_users_count = $shop_users_count + $shop_engineers_count;
        //统计
        $export_detail['order_gross_profit'] = $export_detail['order_payable_amount'] -
            $export_detail['order_fitting_amount'];
        if($export_detail['order_payable_amount'] != 0) {
            $export_detail['order_gross_profit_rate'] =
                round(($export_detail['order_gross_profit'] / $export_detail['order_payable_amount']),6) * 1000000;
        }
        if($export_detail['order_count'] != 0) {
            $export_detail['order_average'] =
                round($export_detail['order_payable_amount'] / $export_detail['order_count'],4) * 10000;
        }
        $export_detail['order_add_cars'] = count($cars_array);
        if($export_detail['order_add_cars'] != 0) {
            $export_detail['order_keep_rate'] = round(($come_again_count / $export_detail['order_add_cars']),6) * 1000000;
        }
        if(count($all_cars_60) != 0) {
            $export_detail['order_rePurchase_rate'] = round((count($all_cars_again_60) / count($all_cars_60)),6) * 1000000;
        }
        if($orders_count_7 != 0) {
            $export_detail['order_stay_sum'] = round(($orders_time_long_7 / 60) / $orders_count_7);
        }
        if($shop_stations_count != 0) {
            $export_detail['order_count_pur_station'] = round($export_detail['order_count'] / $shop_stations_count,4) * 10000;
            $export_detail['order_amount_pur_station'] =
                round($export_detail['order_payable_amount'] / $shop_stations_count,4) * 10000;
        }
        if($shop_users_count != 0) {
            $export_detail['order_count_pur_man'] = round($export_detail['order_count'] / $shop_users_count,4) * 10000;
            $export_detail['order_amount_pur_man'] = round($export_detail['order_payable_amount'] / $shop_users_count,4) * 10000;
        }
        return $export_detail;
    }
    /**
     * @Author:zhanghu
     * @DateTime: 2017-02-11 13:52
     * @Description:计算所有门店当日营业数据（监控数据）分类
     */
    public static function get_shop_control_data_classify($date,$shop_id){
        $begin_date = date('Y-m-d 00:00:00',strtotime($date));
        $end_date = date('Y-m-d 23:59:59',strtotime($date));
        $begin_date_37_31 = date("Y-m-d H:i:s",strtotime($date." 00:00:00") - 37 * 24 * 60 * 60);
        $end_date_37_31 = date("Y-m-d H:i:s",strtotime($date." 23:59:59") - 31 * 24 * 60 * 60);
        $begin_date_60 = date("Y-m-d H:i:s",strtotime($date." 00:00:00") - 60 * 24 * 60 * 60);
        $end_date_60 = $end_date;
        $begin_date_7 = date("Y-m-d H:i:s",strtotime($date." 00:00:00") - 7 * 24 * 60 * 60);
        $end_date_7 = $end_date;
        $payed_array = array(1018,1017,1217,1317,1417);
        // 统计分类
        $classify_array = array();
        $order = Orders::whereIn('status',$payed_array)//完结状态的订单
        ->whereBetween('pay_time',array($begin_date,$end_date))
            ->where('shop_id',$shop_id)
            ->where('place',2)//1.上门服务，2.到店服务
            ->get();
        foreach($order as $ord){
            $this_order_ps = OrderProduct::where('order_id',$ord->order_id)->where("status",1)->get();
            foreach($this_order_ps as $as){
                $classify_array[] = $as->product_id;
            }
        }
        $export_detail = array();
        $classify_array = array_flip(array_flip($classify_array));
        foreach($classify_array as $ca) {
            $product_detail = array(
                'order_count' => 0,//交易台次     已结算订单数量
                'order_payable_amount' => 0,//订单实收总计 分    已结算订单金额
                'order_fitting_amount' => 0,//配件成本 分        已经结算订单中耗材成本
                'order_gross_profit' => 0,//毛利 分             实收-配件成本=毛利
                'order_gross_profit_rate' => 0,//毛利率 %       毛利/实收
                'order_average' => 0,//单均价 分                订单实收/交易台次
                'order_new_cars' => 0,//新增车辆                第一次下单车辆数量
                'order_add_cars' => 0,//累积车辆                累积计算在门店进行服务车辆数，以单一车牌号为准
                'order_rePurchase_rate' => 0,//60日复购率 %     60天内，同一车辆进行2次以上订单的车辆数/累积车辆数
                'order_keep_rate' => 0,//30日留存率 %           向前推37天，37-31天内新增车辆，在30-当天有过交际行为的车辆/累积车辆数
                'order_stay_sum' => 0,//7日平均在场时长 分/单   记录每个订单，从服务开始到订单结算所用时间，时间转为分钟。以当天为准，
                //计算7天内所有订单所用时间时长/7天内订单数
                'order_count_pur_station' => 0,//单工位台次      服务台次/所使用工位数量
                'order_amount_pur_station' => 0,//单工位产值 分  订单实收/所使用工位数量
                'order_count_pur_man' => 0,//人均台次
                'order_amount_pur_man' => 0,//人均产值 分
                'classify_id' => $ca,//分类id
                'classify_name' => Service::find($ca)->name,//产品包名
            );
            $cars_array = Array();
            foreach ($order as $ord1) {
                $this_order_ps1 = OrderProduct::where('order_id', $ord1->order_id)->where("status", 1)->get();
                foreach ($this_order_ps1 as $op) {
                    if ($op->product_id == $ca) {
                        $product_detail['order_count'] += 1;
                        $product_detail['order_payable_amount'] += $op->counter_amount;
                        switch ($op->product_type) {
                            case 'enterprise':
                                $opep=OrderProductEsPackage::with("item")->where("order_product_id",$op->id)->get();
                                foreach ($opep as $key => $value){
                                    foreach ($value->item as $_item) {
                                        if ($_item->type == 1) {
                                            if ($_item->item_type == 0) {
                                                if($_item->is_adapter==1) {
                                                    $item_info = SupplyItem::getCacheItemInfo($_item->item_id);
                                                    $product_detail['order_fitting_amount'] += ($_item->num * $item_info->purchase_price);
                                                }
                                            }else{
                                                $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                                $product_detail['order_fitting_amount'] += ($_item->num * $item_info->outbound_amount);
                                            }
                                        }
                                    }
                                }
                                break;
                            case 'activity':
                                $opap=OrderProductAcPackage::with("item")->where("order_product_id",$op->id)->get();
                                foreach ($opap as $key => $value){
                                    foreach ($value->item as $_item) {
                                        if ($_item->type == 1) {
                                            if ($_item->item_type == 0) {
                                                if($_item->is_adapter==1) {
                                                    $item_info = SupplyItem::getCacheItemInfo($_item->item_id);
                                                    $product_detail['order_fitting_amount'] += ($_item->num * $item_info->purchase_price);
                                                }
                                            }else{
                                                $item_info=SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                                $product_detail['order_fitting_amount'] += ($_item->num * $item_info->outbound_amount);
                                            }
                                        }
                                    }
                                }
                                break;
                            case 'base':
                                $opp=OrderProductBaseItem::where("order_product_package_id",$op->id)->get();
                                foreach ($opp as $key=>$_item) {
                                    if($_item->type==1){
                                        if($_item->item_type==0){
                                            if($_item->is_adapter==1) {
                                                $item_info = SupplyItem::getCacheItemInfo($_item->item_id);
                                                $product_detail['order_fitting_amount'] += ($_item->num * $item_info->purchase_price);
                                            }
                                        }else {
                                            $item_info = SupplyItemNonStandard::getCacheItemInfo($_item->item_id);
                                            $product_detail['order_fitting_amount'] += ($_item->num * $item_info->purchase_price);
                                        }
                                    }
                                }
                                break;
                            case 'non_standard':
                                $opn=OrderProductNonStandard::where("order_product_id",$op->id)->where('status',1)->get();
                                foreach ($opn as $key=>$value) {
                                    if($value->service_type==1){
                                        if($value->type==0){
                                            $item_info=SupplyItem::getCacheItemInfo($value->item_id);
                                            $product_detail['order_fitting_amount'] += ($value->num * $item_info->purchase_price);
                                        }else {
                                            $item_info=SupplyItemNonStandard::getCacheItemInfo($value->item_id);
                                            $product_detail['order_fitting_amount'] += ($value->num * $item_info->purchase_price);
                                        }
                                    }
                                }
                                break;
                            case 'promotion':
                                $opp=OrderProductPackage::with("item")->where("order_product_id",$op->id)->get();
                                foreach ($opp as $key => $value){
                                    foreach ($value->item as $_item) {
                                        if($_item->type==1) {
                                            $item_info=SupplyItem::getCacheItemInfo($_item->item_id);
                                            $product_detail['order_fitting_amount'] += ($_item->num * $item_info->purchase_price);
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
                $car_no = $ord1->car_no;
                $pay_time = $ord1->pay_time;
                $all_orders = Orders::whereIn('status',$payed_array)
                    ->where('pay_time','<',$pay_time)
                    ->where('shop_id',$shop_id)
                    ->where('car_no',$car_no);
                $all_orders_count = $all_orders->count();
                $all_orders = $all_orders->get();
                if($all_orders_count == 0){
                    $product_detail['order_new_cars'] += 1;
                }else {
                    $is_new = 0;
                    foreach ($all_orders as $ao) {
                        $this_order_ps2 = OrderProduct::where('order_id', $ao->order_id)->where("status", 1)->get();
                        foreach ($this_order_ps2 as $op2) {
                            if($op2->product_id == $ca){
                                $is_new = 1;
                            }
                        }
                    }
                    if($is_new == 0){
                        $product_detail['order_new_cars'] += 1;
                    }
                }
                $cars_array[] = $car_no;
            }
            $cars_array = array_flip(array_flip($cars_array));
            //37-31订单
            $order_2 = Orders::whereIn('status',$payed_array)//完结状态的订单
            ->whereBetween('pay_time',array($begin_date_37_31,$end_date_37_31))
                ->where('shop_id',$shop_id)
                ->where('place',2)//1.上门服务，2.到店服务
                ->get();
            $cars_array_2 = array();
            foreach ($order_2 as $_order_2){
                $car_no_2 = $_order_2->car_no;
                $pay_time_2 = $_order_2->pay_time;
                $all_orders1 = Orders::whereIn('status',$payed_array)
                    ->where('pay_time','<',$pay_time_2)
                    ->where('shop_id',$shop_id)
                    ->where('place',2)//1.上门服务，2.到店服务
                    ->where('car_no',$car_no_2);
                $all_orders1_count = $all_orders1->count();
                $all_orders1 = $all_orders1->get();
                if($all_orders1_count == 0){
                    $cars_array_2[] = $car_no_2;
                }else {
                    $is_new1 = 0;
                    foreach ($all_orders1 as $ao1) {
                        $this_order_ps3 = OrderProduct::where('order_id', $ao1->order_id)->where("status", 1)->get();
                        foreach ($this_order_ps3 as $op3) {
                            if($op3->product_id == $ca){
                                $is_new1 = 1;
                            }
                        }
                    }
                    if($is_new1 == 0){
                        $cars_array_2[] = $car_no_2;
                    }
                }
            }
            $cars_array_2 = array_flip(array_flip($cars_array_2));
            $come_again_count = 0;
            foreach ($cars_array_2 as $car_a) {
                if(in_array($car_a,$cars_array)){
                    $come_again_count += 1;
                }
            }
            //60 内订单
            $order_3 = Orders::whereIn('status',$payed_array)//完结状态的订单
            ->whereBetween('pay_time',array($begin_date_60,$end_date_60))
                ->where('shop_id',$shop_id)
                ->where('place',2)//1.上门服务，2.到店服务
                ->get();
            $all_cars_60 = array();
            $all_cars_again_60 = array();
            foreach ($order_3 as $_order_3){
                $this_order_ps4 = OrderProduct::where('order_id', $_order_3->order_id)->where("status", 1)->get();
                foreach ($this_order_ps4 as $op4) {
                    if($op4->product_id == $ca){
                        $car_no_3 = $_order_3->car_no;
                        if(!in_array($car_no_3,$all_cars_60)){
                            $all_cars_60[] = $car_no_3;
                        }else{
                            $all_cars_again_60[] = $car_no_3;
                        }
                    }
                }
            }
            $all_cars_again_60 = array_flip(array_flip($all_cars_again_60));
            //7 内订单
            $order_4 = Orders::whereIn('status',$payed_array)//完结状态的订单
            ->whereBetween('pay_time',array($begin_date_7,$end_date_7))
                ->where('shop_id',$shop_id)
                ->where('place',2)//1.上门服务，2.到店服务
                ->get();
            $orders_count_7 = 0;
            $orders_time_long_7 = 0;
            foreach($order_4 as $_order_4){
                $this_order_ps5 = OrderProduct::where('order_id', $_order_4->order_id)->where("status", 1)->get();
                foreach ($this_order_ps5 as $op5) {
                    if($op5->product_id == $ca){
                        $orders_count_7 += 1;
                        $orders_time_long_7 += (strtotime($_order_4->pay_time) - strtotime($_order_4->start_time));
                    }
                }
            }
            $shop_stations_count = MaintenanceStation::where('status',1)->where('shop_id',$shop_id)->count();
            $shop_users_count = SCUser::where('status',1)->where('shop_id',$shop_id)->count();//门店人员
            $shop_engineers_count = MaintenanceEngineers::where('status',1)->where('type','shop')->where('type_id',$shop_id)->count();//门店技师
            $shop_users_count = $shop_users_count + $shop_engineers_count;
            //统计

            $product_detail['order_gross_profit'] = $product_detail['order_payable_amount'] -
                $product_detail['order_fitting_amount'];
            if($product_detail['order_payable_amount'] != 0) {
                $product_detail['order_gross_profit_rate'] =
                    round(($product_detail['order_gross_profit'] / $product_detail['order_payable_amount']),6) * 1000000;
            }
            if($product_detail['order_count'] != 0) {
                $product_detail['order_average'] =
                    round($product_detail['order_payable_amount'] / $product_detail['order_count'] , 4) * 10000;
            }
            $product_detail['order_add_cars'] = count($cars_array);
            if($product_detail['order_add_cars'] != 0) {
                $product_detail['order_keep_rate'] = round(($come_again_count / $product_detail['order_add_cars']),6) * 1000000;
            }
            if(count($all_cars_60) != 0) {
                $product_detail['order_rePurchase_rate'] = round((count($all_cars_again_60) / count($all_cars_60)),6) * 1000000;
            }
            if($orders_count_7 != 0) {
                $product_detail['order_stay_sum'] = round(($orders_time_long_7 / 60) / $orders_count_7);
            }
            if($shop_stations_count != 0) {
                $product_detail['order_count_pur_station'] = round($product_detail['order_count'] / $shop_stations_count,4) * 10000;
                $product_detail['order_amount_pur_station'] =
                    round($product_detail['order_payable_amount'] / $shop_stations_count,4) * 10000;
            }
            if($shop_users_count != 0) {
                $product_detail['order_count_pur_man'] = round($product_detail['order_count'] / $shop_users_count,4) * 10000;
                $product_detail['order_amount_pur_man'] = round($product_detail['order_payable_amount'] / $shop_users_count,4) * 10000;
            }
            $export_detail[] = $product_detail;
        }
        return $export_detail;
    }
}